How to display a Date object in a specific format using JavaScript?

后端 未结 7 1459
不思量自难忘°
不思量自难忘° 2020-12-03 21:21

I have a Date object and I\'d like to display it in the below format:

var myDate = getDate();
// this format: \"13 Jan 2012 11:00am\";

How

7条回答
  •  眼角桃花
    2020-12-03 22:05

    There is a great JavaScript library that handles this very well, and only 5.5kb minified.

    http://momentjs.com/

    It looks something like this:

    moment().format('MMMM Do YYYY, h:mm:ss a'); // February 25th 2013, 9:54:04 am
    moment().subtract('days', 6).calendar(); // "last Tuesday at 9:53 AM"
    

    You can also pass in dates as a String with a format, or a Date object.

    var date = new Date();
    moment(date); // same as calling moment() with no args
    
    // Passing in a string date
    moment("12-25-1995", "MM-DD-YYYY");
    

    Also has great support for languages other than English, like Russian, Japanese, Arabic, Spanish and others.

    Check out the docs.

提交回复
热议问题