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

JavascriptDate

Javascript Problem Overview


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 ?

Javascript Solutions


Solution 1 - Javascript

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();
}

Solution 2 - Javascript

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.

Solution 3 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArepalli PraveenkumarView Question on Stackoverflow
Solution 1 - JavascriptAfsaView Answer on Stackoverflow
Solution 2 - JavascriptManjarView Answer on Stackoverflow
Solution 3 - JavascriptAlex HoppenView Answer on Stackoverflow