How to output date in javascript in ISO 8601 without milliseconds and with Z
Simple way: console.log( new Date().toISOString().split(‘.’)[0]+”Z” );
Simple way: console.log( new Date().toISOString().split(‘.’)[0]+”Z” );
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
Python 3.7+ As of Python 3.7 there is a method datetime.fromisoformat() which is exactly the reverse for isoformat(). Older Python If you have older Python, then this is the current best “solution” to this question: pip install python-dateutil Then… import datetime import dateutil def getDateTimeFromISO8601String(s): d = dateutil.parser.parse(s) return d
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
This solution makes use of the DateTimeStyles enumeration, and it also works with Z. DateTime d2 = DateTime.Parse(“2010-08-20T15:00:00Z”, null, System.Globalization.DateTimeStyles.RoundtripKind); This prints the solution perfectly.
Update for Java 8 and higher You can now simply do Instant.parse(“2015-04-28T14:23:38.521Z”) and get the correct thing now, especially since you should be using Instant instead of the broken java.util.Date with the most recent versions of Java. You should be using DateTimeFormatter instead of SimpleDateFormatter as well. Original Answer: The explanation below is still valid … 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
Swift 4 • iOS 11.2.1 or later extension ISO8601DateFormatter { convenience init(_ formatOptions: Options) { self.init() self.formatOptions = formatOptions } } extension Formatter { static let iso8601withFractionalSeconds = ISO8601DateFormatter([.withInternetDateTime, .withFractionalSeconds]) } extension Date { var iso8601withFractionalSeconds: String { return Formatter.iso8601withFractionalSeconds.string(from: self) } } extension String { var iso8601withFractionalSeconds: Date? { return Formatter.iso8601withFractionalSeconds.date(from: self) } } … Read more
You could use date “+%Y-%m-%d” Or for a fully ISO-8601 compliant date, use one of the following formats: date -u +”%Y-%m-%dT%H:%M:%SZ” Output: 2011-08-27T23:22:37Z or date +%Y-%m-%dT%H:%M:%S%z Output: 2011-08-27T15:22:37-0800
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 … Read more