Python – Convert string representation of date to ISO 8601
Using dateutil: import dateutil.parser as parser text=”Thu, 16 Dec 2010 12:14:05 +0000″ date = parser.parse(text) print(date.isoformat()) # 2010-12-16T12:14:05+00:00
Using dateutil: import dateutil.parser as parser text=”Thu, 16 Dec 2010 12:14:05 +0000″ date = parser.parse(text) print(date.isoformat()) # 2010-12-16T12:14:05+00:00
The date format model elements are listed in the Datetime Format Models documentation: SELECT to_timestamp_tz (‘2014-09-12T11:53:06+00:00’, ‘YYYY-MM-DD”T”HH24:MI:SSTZH:TZM’) FROM DUAL TO_TIMESTAMP_TZ(‘2014-09-12T11:53:06+00:00′,’YYYY-MM-DD”T”HH24:MI:SSTZH:TZM’) ————————————————————————— 12-SEP-14 11.53.06.000000000 +00:00 The fixed T can be included as a character literal: You can include these characters in a date format model: Punctuation such as hyphens, slashes, commas, periods, and colons Character literals, … Read more
For the strict, full datetime, including milliseconds, per the W3C’s take on the spec.: //– Complete precision: /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/ //– No milliseconds: /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)/ //– No Seconds: /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)/ //– Putting it all together: /(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))|(\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z))/ . Additional variations allowed by the actual ISO 8601:2004(E) doc: /******************************************** ** No time-zone varients: */ //– Complete precision: /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+/ //– No milliseconds: … Read more
Incomplete Regex It’s incomplete as it matches invalid date such as 2013-99-99T04:13:00+00:00. Better solution The regex below won’t match this kind of invalid date (cf. ISO 8601 Date Validation That Doesn’t Suck). You can test with the following code : re = /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/ var testDates = { ‘date’ : “2012-10-06T04:13:00+00:00”, ‘validDate’ : “0785-10-10T04:13:00+00:00”, ‘invalidDate’ : … Read more
Local to ISO 8601: import datetime datetime.datetime.now().isoformat() >>> 2020-03-20T14:28:23.382748 UTC to ISO 8601: import datetime datetime.datetime.utcnow().isoformat() >>> 2020-03-20T01:30:08.180856 Local to ISO 8601 without microsecond: import datetime datetime.datetime.now().replace(microsecond=0).isoformat() >>> 2020-03-20T14:30:43 UTC to ISO 8601 with TimeZone information (Python 3): import datetime datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat() >>> 2020-03-20T01:31:12.467113+00:00 UTC to ISO 8601 with Local TimeZone information without microsecond (Python 3): import datetime datetime.datetime.now().astimezone().replace(microsecond=0).isoformat() >>> 2020-03-20T14:31:43+13:00 Local to … Read more
UPDATE 2021 In Python 3.2 timezone was added to the datetime module allowing you to easily assign a timezone to UTC. >>> import datetime >>> n = datetime.datetime.now(datetime.timezone.utc) >>> n.isoformat() ‘2021-07-13T15:28:51.818095+00:00’ previous answer: Timezones are a pain, which is probably why they chose not to include them in the datetime library. try pytz, it has … Read more
Use NSDateFormatter: NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@”en_US_POSIX”]; [dateFormatter setLocale:enUSPOSIXLocale]; [dateFormatter setDateFormat:@”yyyy-MM-dd’T’HH:mm:ssZZZZZ”]; [dateFormatter setCalendar:[NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian]]; NSDate *now = [NSDate date]; NSString *iso8601String = [dateFormatter stringFromDate:now]; And in Swift: let dateFormatter = DateFormatter() let enUSPosixLocale = Locale(identifier: “en_US_POSIX”) dateFormatter.locale = enUSPosixLocale dateFormatter.dateFormat = “yyyy-MM-dd’T’HH:mm:ssZZZZZ” dateFormatter.calendar = Calendar(identifier: .gregorian) let iso8601String = … Read more
JodaTime’s DateTimeFormat to rescue: String dateString = “2010-03-01T00:00:00-08:00”; String pattern = “yyyy-MM-dd’T’HH:mm:ssZ”; DateTimeFormatter dtf = DateTimeFormat.forPattern(pattern); DateTime dateTime = dtf.parseDateTime(dateString); System.out.println(dateTime); // 2010-03-01T04:00:00.000-04:00 (time and timezone difference in toString() is just because I’m at GMT-4 and didn’t set locale explicitly) If you want to end up with java.util.Date just use DateTime#toDate(): Date date = dateTime.toDate(); … Read more
Sort Lexicographically: As @kdbanman points out, ISO8601See General principles was designed for lexicographical sort. As such the ISO8601 string representation can be sorted like any other string, and this will give the expected order. ‘2007-01-17T08:00:00Z’ < ‘2008-01-17T08:00:00Z’ === true So you would implement: var myArray = [ { name:’oldest’, date:’2007-01-17T08:00:00Z’ }, { name:’newest’, date:’2011-01-28T08:00:00Z’ }, … Read more
There is a (reasonably) simple way to parse an ISO timestamp WITHOUT the time zone using formulas instead of macros. This is not exactly what the original poster has asked, but I found this question when trying to parse ISO timestamps in Excel and found this solution useful, so I thought I would share it … Read more