At the moment I save my date like this: ISODate("2014-11-17T16:19:16.224Z")
, but I want this result: ISODate("2014-11-16T23:00:00Z")
. How can I do this?
At the moment I save my date like this: ISODate("2014-11-17T16:19:16.224Z")
, but I want this result: ISODate("2014-11-16T23:00:00Z")
. How can I do this?
- 4 Set the parts you don't want saved to 0. In your example, you would set the minutes and seconds to 0. – forgivenson Commented Nov 17, 2014 at 16:27
- Can you please give me an example? – user3475602 Commented Nov 17, 2014 at 16:30
4 Answers
Reset to default 6An easier alternative is to use Date.setHours()
- in single call you can set what you need - from hours to milliseconds. If you just want to get rid of the time.
var date = new Date();
date.setHours(0,0,0,0);
console.log ( date );
Set the parts you don't want saved to 0. In your example, you would set the minutes, seconds, and milliseconds to 0.
var date = new Date();
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
var isoDateString = date.toISOString();
console.log(isoDateString);
Or, a less verbose option:
var date = new Date();
var isoDateString = date.toISOString().substring(0,10);
console.log(isoDateString);
To Save a date without a time stamp:
let date = new Date().toLocaleDateString('en-US');
console.log(date)
// OUTPUT -> m/d/yyyy
Use this to find options to add as paramaters for the toLocaleDateString
function