I want to change a date's format in JavaScript. I tried
var today = new Date();
today.toLocaleFormat('%d-%b-%Y');
but that didn't work. How can I approach this problem?
I want to change a date's format in JavaScript. I tried
var today = new Date();
today.toLocaleFormat('%d-%b-%Y');
but that didn't work. How can I approach this problem?
I think there is no straight way to do so. Let's check these out:
var date = new Date();
var options = {
weekday: "long", year: "numeric", month: "short",
day: "numeric", hour: "2-digit", minute: "2-digit"
};
//alert(date.toLocaleDateString("en-US"));
alert(date.toLocaleTimeString("en-us", options));
And I think you are looking for this:
var myDate = new Date();
alert(myDate.getDate() + "-" + (myDate.getMonth() + 1)+ "-" + myDate.getFullYear());
Yes, I've googled and got this solution. Please check it out. :) Thanks!
According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleFormat, it is not recommended to use the above function.
Check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString instead.
date = new Date();
date.format('dd st-MMM-yyyy');
Here's an article that explains it in depth.