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?
- 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
2 Answers
Reset to default 3Here 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