javascript - How to set Hours,minutes,seconds to Date which is in GMT - Stack Overflow

I have Date Object ,I wanted to clear HOUR,MINUTE and SECONDS from My Date.Please help me how to do it

I have Date Object ,I wanted to clear HOUR,MINUTE and SECONDS from My Date.Please help me how to do it in Javascript. Am i doing wrong ?

var date = Date("Fri, 26 Sep 2014 18:30:00 GMT");
      date.setHours(0);
      date.setMinutes(0);
      date.setSeconds(0);

Expected result is

Fri, 26 Sep 2014 00:00:00 GMT

How Do I achieve ?

I have Date Object ,I wanted to clear HOUR,MINUTE and SECONDS from My Date.Please help me how to do it in Javascript. Am i doing wrong ?

var date = Date("Fri, 26 Sep 2014 18:30:00 GMT");
      date.setHours(0);
      date.setMinutes(0);
      date.setSeconds(0);

Expected result is

Fri, 26 Sep 2014 00:00:00 GMT

How Do I achieve ?

Share Improve this question edited Mar 6, 2015 at 6:42 Arepalli Praveenkumar asked Sep 4, 2014 at 10:48 Arepalli PraveenkumarArepalli Praveenkumar 1,3071 gold badge13 silver badges29 bronze badges 0
Add a comment  | 

3 Answers 3

Reset to default 138

According to MDN the setHours function actually takes additional optional parameters to set both minutes, seconds and milliseconds. Hence we may simply write

// dateString is for example "Fri, 26 Sep 2014 18:30:00 GMT"
function getFormattedDate(dateString) {
   var date = new Date(dateString);
   date.setHours(0, 0, 0);   // Set hours, minutes and seconds
   return date.toString();
}

You can use this:

// Like Fri, 26 Sep 2014 18:30:00 GMT
var today = new Date();

var myToday = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0);

Recreate the Date object with constructor using the actual date.

To parse the date into JavaScript simply use

var date = new Date("Fri, 26 Sep 2014 18:30:00 GMT”);

And then set Hours, Minutes and seconds to 0 with the following lines

date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);

date.toString() now returns your desired date

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1736914281a3915431.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信