Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Hours,minutes,seconds to Date which is in GMT

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 ?

like image 568
Arepalli Praveenkumar Avatar asked Sep 04 '14 10:09

Arepalli Praveenkumar


People also ask

How do you calculate hours and minutes from dates?

To get the hours and minutes from a date, call the getHours() and getMinutes() methods on the date object. The getHours method returns the hours and the getMinutes() method returns the minutes in the specified date.

How do you represent hours minutes and seconds?

Usually, hours, minutes, and seconds are abbreviated as h, min, and s. Minute can also be written as m if there is no risk of confusion with the meter. For time, you can use : as a separator, as in “Meet me at 12:50 PM”, or “The world record for a full marathon is 2:01:39”. After hour, minute, and second, what is next?

How do you use setHours?

Use the setHours() method to set a date's hours, minutes and seconds to 0 , e.g. date. setHours(0, 0, 0, 0) . The setHours method takes the hours, minutes, seconds and milliseconds as parameters and sets the values on the date.


1 Answers

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(); } 
like image 145
Afsa Avatar answered Sep 18 '22 18:09

Afsa