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

javascript - How to get date with 00:00:00 timestamp - Stack Overflow

programmeradmin3浏览0评论

I am trying to get the date in this format:

Thu Nov 01 2018 00:00:00 GMT+0530 (India Standard Time)

But I'm getting in this format:

Thu Nov 01 2018 05:30:00 GMT+0530 (India Standard Time)

How can I get the desired output?

this is what I tried:

new Date (item.expDate+ 'T00:00:00Z');

I am trying to get the date in this format:

Thu Nov 01 2018 00:00:00 GMT+0530 (India Standard Time)

But I'm getting in this format:

Thu Nov 01 2018 05:30:00 GMT+0530 (India Standard Time)

How can I get the desired output?

this is what I tried:

new Date (item.expDate+ 'T00:00:00Z');
Share Improve this question asked Nov 1, 2018 at 21:29 Mr world wideMr world wide 4,85410 gold badges45 silver badges105 bronze badges 8
  • 1 what code are you using to get the first result? – Jhecht Commented Nov 1, 2018 at 21:39
  • 1 because as far as I know you should be able to use new Date(year, month, date, 0,0,0) to set the hour/min/seconds to zero for the given values year, month, date in your code – Jhecht Commented Nov 1, 2018 at 21:40
  • 3 you can also set the hour/minute/seconds after creation like so var d = new Date(); d.setHours(0,0,0) – Jhecht Commented Nov 1, 2018 at 21:41
  • 1 setHours worked. ! thanks @Jhecht – Mr world wide Commented Nov 1, 2018 at 21:49
  • 2 @Mrworldwide—you're setting the time to 00:00:00 UTC but the string is local, so it's offset by the host timezone offset (which is +0530 apparently). BTW, if item.expDate is in YYYY-MM-DD format, it will be parsed as UTC anyway (a rather silly idiosyncrasy of ECMAScript), adding "T00:00:00Z" does nothing useful. – RobG Commented Nov 1, 2018 at 23:16
 |  Show 3 more ments

1 Answer 1

Reset to default 4

You can create a new Date object and use setHours, setMinutes, and setSeconds to set the hours, minutes, and seconds to 0.

var d = new Date;
d.setHours(0);
d.setMinutes(0);
d.setSeconds(0);
console.log(d);

This could be bined into one setHours call.

var d = new Date;
d.setHours(0, 0, 0); // arguments are hours, minutes, seconds
console.log(d.toString());

You can also just use the Date constructor like this:

new Date(year, month, day, hours, minutes, seconds, milliseconds);

console.log(new Date(2018, 10, 1, 0, 0, 0)); 

发布评论

评论列表(0)

  1. 暂无评论