I am creating a node.js endpoint that accepts a start date/time and end date/time as parameters. I had been passing them as a string ie:
var body = {
relatedObjectId: "561ee6bbe4b0f25b4aead5c8",
startTime : "11/13/2015 03:00:00PM",
endTime: "11/13/2015 03:30:00PM"
};
and in my service class:
var timeTicket = new TimeTicket();
timeTicket.tutorId = tutorId;
timeTicket.startTime = new Date(startTime);
timeTicket.endTime = new Date(endTime);
timeTicket.save(function(err, timeTicket){
if(err){
return next(err, null);
}
return next(null, timeTicket);
});
However, the cast always fails so i end up with a date in 1970 for startTime and endTime values. It would seem the obvious solution would be to use a UTC format, but what's the right way to do that?
I am creating a node.js endpoint that accepts a start date/time and end date/time as parameters. I had been passing them as a string ie:
var body = {
relatedObjectId: "561ee6bbe4b0f25b4aead5c8",
startTime : "11/13/2015 03:00:00PM",
endTime: "11/13/2015 03:30:00PM"
};
and in my service class:
var timeTicket = new TimeTicket();
timeTicket.tutorId = tutorId;
timeTicket.startTime = new Date(startTime);
timeTicket.endTime = new Date(endTime);
timeTicket.save(function(err, timeTicket){
if(err){
return next(err, null);
}
return next(null, timeTicket);
});
However, the cast always fails so i end up with a date in 1970 for startTime and endTime values. It would seem the obvious solution would be to use a UTC format, but what's the right way to do that?
Share Improve this question asked Nov 13, 2015 at 1:30 Eric HEric H 1,3533 gold badges15 silver badges29 bronze badges 4-
2
Use an ISO 8601 format, eg
"2015-11-13T15:00:00.000Z"
– Phil Commented Nov 13, 2015 at 1:32 - 1 Generally, parsing of strings with the Date constructor is unreliable and should not be used. However, Node.js environments might be sufficiently standardised for ISO 8601 UTC formatted strings to be reliable. – RobG Commented Nov 13, 2015 at 1:45
- @RobG, are you suggesting using the number based format is superior? Under what conditions would it be unreliable to parse a string? – Eric H Commented Nov 13, 2015 at 20:04
- 1 @EricH—if by "number based" you mean passing a time value (e.g. seconds or milliseconds since 1970-01-01T00:00:00Z) then yes. Parsing of strings using the Date constructor (which is the same as using Date.parse) has always been problematic in ECMAScript. However, Node.js doesn't carry as much baggage as browsers and code is generally written for one environment rather than the unknown plethora of javascript implementations, so it may be OK to use ISO 8601 format. – RobG Commented Nov 14, 2015 at 1:45
1 Answer
Reset to default 5Either use ISO 8601 format as Phil suggested, or simply pass the date as milliseconds (since 1970).
For example, new Date(1447378736842)
is the same as new Date("2015-11-13T01:38:56.842Z")
.
To get the current date in ISO 8601 format, you might do something like this:
var d = new Date();
var n = d.toISOString();
tl;dr version: This is what your body
object should look like. I used milliseconds for startTime
and an ISO 8601 string for endTime
for demonstration purposes. Both are valid.
var body = {
relatedObjectId: "561ee6bbe4b0f25b4aead5c8",
startTime : 1447378736842,
endTime: "2015-11-13T01:38:56.842Z"
};