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!
Please find below my answer ,
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10)
{
dd='0'+dd
}
if(mm<10)
{
mm='0'+mm
}
var today = dd+'/'+mm+'/'+yyyy;
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.