Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  134] [ 5]  / answers: 1 / hits: 147206  / 14 Years ago, fri, january 28, 2011, 12:00:00

I need help/tips on converting an ISO 8601 date with the following structure into JavaScript:


CCYY-MM-DDThh:mm:ssTZD

I'd like to format the date like so:


January 28, 2011 - 7:30PM EST

I'd like to keep this solution as clean and minimal as possible.


More From » datetime

 Answers
8

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

[#94009] Thursday, January 27, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darrell

Total Points: 109
Total Questions: 113
Total Answers: 113

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
;