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
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
The format you’re describing is ISO 8601. Since you’re working with timestamps that inclulde a time zone component, I’d strongly recommend using DateTimeOffset instead of DateTime. It makes things so much easier! To create a DateTimeOffset for a given date, time, and time zone offset, use this syntax: var date = new DateTimeOffset(2016, 3, 29, … Read more
Option: isoformat() Python’s datetime does not support the military timezone suffixes like ‘Z’ suffix for UTC. The following simple string replacement does the trick: In [1]: import datetime In [2]: d = datetime.datetime(2014, 12, 10, 12, 0, 0) In [3]: str(d).replace(‘+00:00’, ‘Z’) Out[3]: ‘2014-12-10 12:00:00Z’ str(d) is essentially the same as d.isoformat(sep=’ ‘) See: Datetime, … 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
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
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
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