Format Date() In Js
I have script then needs to get the date and time in this format 08/25/2017 1:54 PM I write code for date Here it is $(document).ready(function() { var dateNow = new Date();
Solution 1:
You can directly use :
dateNow.getHours()
dateNow.getMinutes()
Solution 2:
This code should work for you.
It formats the date in your required format 08/25/2017 1:54 PM
var dateNow = newDate();
var dd = dateNow.getDate();
var mm = dateNow.getMonth()+1; //January is 0!var yyyy = dateNow.getFullYear();
if(dd<10) {
dd = '0' + dd;
}
if(mm<10) {
mm = '0' + mm;
}
var h = dateNow.getHours(), m = dateNow.getMinutes();
var _time = (h > 12) ? (h-12 + ':' + m +' PM') : (h + ':' + m +' AM');
dateNow = mm + '/' + dd + '/' + yyyy+ ' ' + _time;
alert(dateNow);
Solution 3:
Use this code:
var date = newDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
var str = (date.getMonth() + 1) + "/" +date.getDate() + "/" + date.getFullYear()+ " " + strTime;
console.log(str);
Post a Comment for "Format Date() In Js"