最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to change Date format in JavaScript? - Stack Overflow

programmeradmin5浏览0评论

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?

Share Improve this question edited Jan 20, 2015 at 6:01 royhowie 11.2k14 gold badges53 silver badges67 bronze badges asked Jan 20, 2015 at 5:53 vijayvijay 1,4952 gold badges16 silver badges26 bronze badges 4
  • 1 possible duplicate of how to format javascript date – Vivek Commented Jan 20, 2015 at 5:55
  • Why not working , its correct only mate. It should work – Dimag Kharab Commented Jan 20, 2015 at 5:56
  • stackoverflow./questions/17032735/… You can find the solution here ;) it worked for me. – Sri Chakra 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 inpatibilities between implementations and the behavior may change in the future." – Rakesh_Kumar Commented Jan 20, 2015 at 6:02
Add a ment  | 

4 Answers 4

Reset to default 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!

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/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleFormat, it is not remended to use the above function.

Check out https://developer.mozilla/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.

发布评论

评论列表(0)

  1. 暂无评论