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 ?
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.
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?
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.
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With