最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Save date 'without' time in JavaScript - Stack Overflow

programmeradmin4浏览0评论

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?

Share Improve this question asked Nov 17, 2014 at 16:25 user3475602user3475602 1,2172 gold badges21 silver badges43 bronze badges 2
  • 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
Add a ment  | 

4 Answers 4

Reset to default 6

An 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

发布评论

评论列表(0)

  1. 暂无评论