-1

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?

4
  • 1
    possible duplicate of how to format javascript date Commented Jan 20, 2015 at 5:55
  • Why not working , its correct only mate. It should work Commented Jan 20, 2015 at 5:56
  • stackoverflow.com/questions/17032735/… You can find the solution here ;) it worked for me. Commented Jan 20, 2015 at 6:00
  • toLocaleFormat method of Date object is deprecated...This what link suggests "This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future." Commented Jan 20, 2015 at 6:02

4 Answers 4

3

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!

Sign up to request clarification or add additional context in comments.

Comments

2

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; 

Comments

0

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.

Comments

-1
date = new Date();
date.format('dd st-MMM-yyyy'); 

Here's an article that explains it in depth.

1 Comment

format is not a pre-defined method on Date object in javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.