JavaScript's date constructor can parse strings to create a date:
var date = new Date("2015");
console.log(date); // Thu Jan 01 2015 06:00:00 GMT+0600 (NOVT)
console.log(date.getTime()); // 1420070400000
I need a similar parsing (string to date) in my C++ Node.js addon. I found two ways to get v8:Date:
static Local<Value> Date::New(Isolate* isolate, double time)
. It takes a double value.static Date* Date::Cast(v8::Value* obj)
But it simple converts string to double:
v8::Local<v8::String> str = Nan::New("2015").ToLocalChecked();
v8::Date *castDate = v8::Date::Cast(*str);
double castVal = castDate->NumberValue();
printf("%f\n", castVal); // 2015.000000, not 1420070400000 :(
v8::Local<v8::Date> newDate =
v8::Date::New(info.GetIsolate(), 2015).As<v8::Date>();
double newVal = newDate->NumberValue();
printf("%f\n", newVal); // 2015.000000
What methods are there in v8 for creating C++ v8::Date from a string?
UPDATE (2016.01.05):
I added "without JS execution from C++" to the question title.