Javascript reformat date string

前端 未结 5 1952
再見小時候
再見小時候 2020-12-21 01:14

I have this date fromat:

Mon Feb 02 2015 05:18:44 GMT+0000 (UTC) 

How can I reformat it to something more friendlier such as 2/2/2015

相关标签:
5条回答
  • 2020-12-21 01:52

    If you want the simplest way just concat all of the separate parts

    var output = dt.getMonth( ) + 1 +'/'+ dt.getDate( ) + '/' +dt.getFullYear( );
    

    There are a few libraries that will handle more advanced stuff if you need it, but that is the lightest way I can think of doing what you are asking.

    0 讨论(0)
  • 2020-12-21 01:53

    Use this awesome library. Moment JS

    For your case it would be some thing link

    var dateString = 'Mon Feb 02 2015 05:18:44 GMT+0000';
    var date = new Moment(dateString);
    
    alert(date.format('MM/dd/YYYY'));
    
    0 讨论(0)
  • 2020-12-21 01:53

    There are many ways to parse and re-format a date. Here is a very simple method that works for your date format:

    var d = 'Mon Feb 02 2015 05:18:44 GMT+0000 (UTC)';
    var dStr = new Date(d).toDateString();
    

    The value of dSTr will be something like 'Sun Feb 01 2015'.

    0 讨论(0)
  • 2020-12-21 01:56

    var d = new Date(); var n = d.toLocaleDateString();

    I think this will give you desired output.

    0 讨论(0)
  • 2020-12-21 02:03

    You can pass it to Date Object:

    var dateString = "Mon Feb 02 2015 05:18:44 GMT+0000 (UTC)";
    var date = new Date(dateString);
    
    date.getDate(); // > 2 (the day number)
    date.getMonth(); // > 1 (the month number as 0 is January, 11 is December)
    

    You can also find a lib to do the format job or format yourself.

    0 讨论(0)
提交回复
热议问题