Parsing ISO 8601 date in Javascript
datejs could parse following, you might want to try out.
Date.parse('1997-07-16T19:20:15') // ISO 8601 Formats
Date.parse('1997-07-16T19:20:30+01:00') // ISO 8601 with Timezone offset
Edit: Regex version
x = "2011-01-28T19:30:00EST"
MM = ["January", "February","March","April","May","June","July","August","September","October","November", "December"]
xx = x.replace(
/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):\d{2}(\w{3})/,
function($0,$1,$2,$3,$4,$5,$6){
return MM[$2-1]+" "+$3+", "+$1+" - "+$4%12+":"+$5+(+$4>12?"PM":"AM")+" "+$6
}
)
Result
January 28, 2011 - 7:30PM EST
Edit2: I changed my timezone to EST and now I got following
x = "2011-01-28T19:30:00-05:00"
MM = {Jan:"January", Feb:"February", Mar:"March", Apr:"April", May:"May", Jun:"June", Jul:"July", Aug:"August", Sep:"September", Oct:"October", Nov:"November", Dec:"December"}
xx = String(new Date(x)).replace(
/\w{3} (\w{3}) (\d{2}) (\d{4}) (\d{2}):(\d{2}):[^(]+\(([A-Z]{3})\)/,
function($0,$1,$2,$3,$4,$5,$6){
return MM[$1]+" "+$2+", "+$3+" - "+$4%12+":"+$5+(+$4>12?"PM":"AM")+" "+$6
}
)
return
January 28, 2011 - 7:30PM EST
Basically
String(new Date(x))
return
Fri Jan 28 2011 19:30:00 GMT-0500 (EST)
regex parts just converting above string to your required format.
January 28, 2011 - 7:30PM EST
JavaScript ISO 8601 string into Date object
The
2014-03-03T...
notation is a fancy JavaScript Date Time String Format and expects a time zone. If you don't provide one, it defaults toZ
(UTC).The
2014-03-03 18:30:00
notation, however, is just a regular string without an interesting name and, if you don't provide a time zone, it assumes local time.
This info was taken from the MDN article about Date.parse()
.
Parsing ISO-8601 date containing TZ offset with seconds
Seems there is a problem with definition of ISO date format in Javascript.
Javascript is based on ECMA-262, which is published by ECMA International. ISO 8601 is published by ISO, a different standards organisation. I don't have a full copy of ISO 8601, however I expect that there are extensions to allow seconds in the offset given they were fairly common prior to 1900.
ECMA-262 defines a couple of formats, one is based on a simplification of ISO 8601, so does not support all of the formats ISO 8601 does (the other is the format produced by toString).
Since the format in the OP is not consistent with one of the formats in ECMA-262, parsing is implementation dependent, see Why does Date.parse give incorrect results?
Your best option is to manually parse the string, either with a simple function or a library that supports seconds in the offset.
An example parse function is below (according to timeanddate the seconds part of the offset in 1919 should have been 19, not 17 so I've modified the original string accordingly):
// Parse format 1919-07-01T00:00:00+04:31:19
function parseWithTZSecs(date) {
let [Y, M, D, H, m, s, oH, om, os] = date.match(/\d+/g);
let sign = date.substring(19,20) == '+'? -1 : 1;
return new Date(Date.UTC(Y, M-1, D, +H + sign*oH, +m + sign*om, +s + sign*os));
}
let date = parseWithTZSecs('1919-07-01T00:00:00+04:31:19');
// 1919-07-01, 12:00:00 AM
console.log(date.toLocaleString('en-CA',{timeZone: 'Europe/Moscow'}));
How to parse ISO 8601 into date and time format using Moment js in Javascript?
With moment.js
var str = '2011-04-11T10:20:30Z';var date = moment(str);var dateComponent = date.utc().format('YYYY-MM-DD');var timeComponent = date.utc().format('HH:mm:ss');console.log(dateComponent);console.log(timeComponent);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
Parse ISO 8601 basic datetime format in javascript
Update
If you can't use moment.js and you can rely on the format of your input being as described, then consider parsing it yourself with something like this:
function parseDate(input) { return new Date(Date.UTC( parseInt(input.slice(0, 4), 10), parseInt(input.slice(4, 6), 10) - 1, parseInt(input.slice(6, 8), 10), parseInt(input.slice(9, 11), 10), parseInt(input.slice(11, 13), 10), parseInt(input.slice(13,15), 10) ));}
console.log(parseDate('20130208T080910Z'));
How do I format a date as ISO 8601 in moment.js?
moment().toISOString(); // or format() - see below
http://momentjs.com/docs/#/displaying/as-iso-string/
Update
Based on the answer: by @sennet and the comment by @dvlsg (see Fiddle) it should be noted that there is a difference between format
and toISOString
. Both are correct but the underlying process differs. toISOString
converts to a Date object, sets to UTC then uses the native Date prototype function to output ISO8601 in UTC with milliseconds (YYYY-MM-DD[T]HH:mm:ss.SSS[Z]
). On the other hand, format
uses the default format (YYYY-MM-DDTHH:mm:ssZ
) without milliseconds and maintains the timezone offset.
I've opened an issue as I think it can lead to unexpected results.
JavaScript: Which browsers support parsing of ISO-8601 Date String with Date.parse
I had this issue today. I found momentjs was a good way of parsing ISO 8601 dates in a cross browser manor.
momentjs can also be used to output the date in a different format.
How to convert an ISO 8601 date to '/Date(1525687010053)/' format in javascript?
I don't understand your question, but your code is wrong. There is no Date.parseDate()
function in javascript, only Date.parse()
:
var datevalue = '9999-12-31T00:00:00Z'; var converteddate = Date.parse(datevalue);
document.getElementById('result').innerHTML = converteddate;console.log(converteddate)
<p id="result"></p>
Related Topics
Wait for Background Images in CSS to Be Fully Loaded
How to Use Materialize-CSS with Angular
Remove/Reset Inherited CSS from an Element
Div Square, Width Size Based on 100% Height
How to Copy Text to the Client's Clipboard Using Jquery
How to Get JavaScript Caller Function Line Number and Caller Source Url
Mime Type Error with Express.Static and CSS Files
Filling Empty Cells in CSS Grid Layout
Inherited CSS Values via JavaScript
Differencebetween Angular-Route and Angular-Ui-Router
How to Check for Null Values in JavaScript
Difference Between Throttling and Debouncing a Function
Changed Id Not Being Found by Jquery
Find All Possible Subset Combos in an Array
Official Information on 'Arguments' in Es6 Arrow Functions
How to Print HTML Content on Click of a Button, But Not the Page