-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?

royhowie
  • 10,605
  • 14
  • 45
  • 66
vijay
  • 1,477
  • 15
  • 26
  • 1
    possible duplicate of [how to format javascript date](http://stackoverflow.com/questions/3552461/how-to-format-javascript-date) – Vivek Jan 20 '15 at 05:55
  • Why not working , its correct only mate. It should work – Dimag Kharab Jan 20 '15 at 05:56
  • http://stackoverflow.com/questions/17032735/javascript-change-date-format-from-yyyy-mm-dd-hhmmss-to-mm-dd-yyyy You can find the solution here ;) it worked for me. – Sri Chakra Jan 20 '15 at 06: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." – Rakesh_Kumar Jan 20 '15 at 06:02

4 Answers4

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!

rony36
  • 3,073
  • 1
  • 27
  • 40
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; 
Lajja Thaker
  • 1,909
  • 8
  • 28
  • 52
0
date = new Date();
date.format('dd st-MMM-yyyy'); 

Here's an article that explains it in depth.

royhowie
  • 10,605
  • 14
  • 45
  • 66
user2237567
  • 62
  • 1
  • 6