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

javascript - How to convert Date in moment js - Stack Overflow

programmeradmin8浏览0评论

I am new to Moment js, and I have date in this format: Mon Jul 14 00:00:00 EDT 2014, and I want to convert this date to MMM DD HH:mm Z format, can some one please help me in doing this?

I am able to convert to required format of my date when I specify with out time zone, but when I specify time zone it is showing wrong time, like below:

Jul 14 09:30 +05:30

If I use this code :

var dateStr = "Mon Jul 14 00:00:00 EDT 2014";
var momentDateFormatted = moment(dateStr).format("MMM DD HH:mm Z");

it is showing wrong time.

If I use this code :

var dateStr = "Mon Jul 14 00:00:00 2014";
var momentDateFormatted = moment(dateStr).format("MMM DD HH:mm Z");

by removing EDT (time Zone): it is showing correctly, why? Is there any way that I can pass time zone and get it correctly?

I am new to Moment js, and I have date in this format: Mon Jul 14 00:00:00 EDT 2014, and I want to convert this date to MMM DD HH:mm Z format, can some one please help me in doing this?

I am able to convert to required format of my date when I specify with out time zone, but when I specify time zone it is showing wrong time, like below:

Jul 14 09:30 +05:30

If I use this code :

var dateStr = "Mon Jul 14 00:00:00 EDT 2014";
var momentDateFormatted = moment(dateStr).format("MMM DD HH:mm Z");

it is showing wrong time.

If I use this code :

var dateStr = "Mon Jul 14 00:00:00 2014";
var momentDateFormatted = moment(dateStr).format("MMM DD HH:mm Z");

by removing EDT (time Zone): it is showing correctly, why? Is there any way that I can pass time zone and get it correctly?

Share Improve this question edited Jul 5, 2022 at 6:30 Nicholas K 15.4k8 gold badges35 silver badges59 bronze badges asked Dec 9, 2015 at 20:45 user2108383user2108383 2771 gold badge12 silver badges26 bronze badges 2
  • To reliably parse a string, you must tell the parser the format, or conform to a format you know it parses correctly. – RobG Commented Dec 9, 2015 at 21:05
  • 2 Possible duplicate of Use moment.js to convert date format – hdost Commented Dec 9, 2015 at 21:30
Add a ment  | 

2 Answers 2

Reset to default 3

Here is some working tested code on js fiddle I created http://jsfiddle/markd116/kj5nzz4w/

var dateStr = "Mon Jul 14 00:00:00 EDT 2014";
var momentDateFormatted = moment(dateStr).format("MMM DD HH:mm Z");
jQuery("div").text(momentDateFormatted);

Outputs Jul 14 00:00 -04:00

Found this code:

$(function () {
    $('.date').each(function (index, dateElem) {
        var $dateElem = $(dateElem);
        var formatted = moment($dateElem.text(), 'MM-DD-YYYY').format('MMMM D');
        $dateElem.text(formatted);
    })
});​

Here: Use moment.js to convert date format

Also, here are format types:

2013-02-08T09            # An hour time part separated by a T
2013-02-08 09            # An hour time part separated by a space
2013-02-08 09:30         # An hour and minute time part
2013-02-08 09:30:26      # An hour, minute, and second time part
2013-02-08 09:30:26.123  # An hour, minute, second, and millisecond time part
2013-02-08 24:00:00.000  # hour 24, minute, second, millisecond equal 0 means next day at midnight

2013-02-08  # A calendar date part
2013-W06-5  # A week date part
2013-039    # An ordinal date part

2013-02-08 09+07:00            # +-HH:mm
2013-02-08 09-0100             # +-HHmm
2013-02-08 09Z                 # Z
2013-02-08 09:30:26.123+07:00  # +-HH:mm

With more examples at: http://momentjs./docs/

Hope this answers your question

发布评论

评论列表(0)

  1. 暂无评论