nsdateformatter

Swift ISO8601 format to Date

陌路散爱 提交于 2020-08-19 12:19:50
问题 I have the following string: 20180207T124600Z How can I turn this into a Date object? Here is my current code but it returns nil : let dateString = "20180207T124600Z" let dateFormatter = ISO8601DateFormatter() dateFormatter.formatOptions = .withFullTime print(dateFormatter.date(from: dateString)) 回答1: You can specify ISO8601 date formate to the NSDateFormatter to get Date: let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyyMMdd'T'HHmmssZ" print(dateFormatter.date(from:

Convert UTC NSDate into Local NSDate not working

霸气de小男生 提交于 2020-07-23 05:28:04
问题 I have a string getting from server as "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" I convert this string into NSDate by this formate. class func convertUTCDateToLocateDate(dateStr:String) -> NSDate{ let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" dateFormatter.timeZone = NSTimeZone(name: "UTC") let date = dateFormatter.dateFromString(dateStr) dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" dateFormatter.timeZone = NSTimeZone.localTimeZone() let

Convert UTC NSDate into Local NSDate not working

▼魔方 西西 提交于 2020-07-23 05:27:45
问题 I have a string getting from server as "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" I convert this string into NSDate by this formate. class func convertUTCDateToLocateDate(dateStr:String) -> NSDate{ let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" dateFormatter.timeZone = NSTimeZone(name: "UTC") let date = dateFormatter.dateFromString(dateStr) dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" dateFormatter.timeZone = NSTimeZone.localTimeZone() let

Convert UTC NSDate into Local NSDate not working

北慕城南 提交于 2020-07-23 05:26:21
问题 I have a string getting from server as "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" I convert this string into NSDate by this formate. class func convertUTCDateToLocateDate(dateStr:String) -> NSDate{ let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" dateFormatter.timeZone = NSTimeZone(name: "UTC") let date = dateFormatter.dateFromString(dateStr) dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" dateFormatter.timeZone = NSTimeZone.localTimeZone() let

Convert UTC NSDate into Local NSDate not working

人走茶凉 提交于 2020-07-23 05:26:21
问题 I have a string getting from server as "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" I convert this string into NSDate by this formate. class func convertUTCDateToLocateDate(dateStr:String) -> NSDate{ let dateFormatter = NSDateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" dateFormatter.timeZone = NSTimeZone(name: "UTC") let date = dateFormatter.dateFromString(dateStr) dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" dateFormatter.timeZone = NSTimeZone.localTimeZone() let

How do you get last weeks date in swift in YYYY-MM-DD format?

こ雲淡風輕ζ 提交于 2020-07-15 05:11:48
问题 How can I display date of one week ago in the format of YYYY-MM-DD like this one "2015-02-18" in Swift 回答1: You can use Calendar's date(byAdding component:) to calculate today minus a week and then you can format your date as desired using DateFormatter: let lastWeekDate = Calendar.current.date(byAdding: .weekOfYear, value: -1, to: Date())! let dateFormatter = DateFormatter() dateFormatter.locale = Locale(identifier: "en_US_POSIX") dateFormatter.dateFormat = "yyyy-MM-dd" let

Swift: Get correct time zone from Date Picker?

和自甴很熟 提交于 2020-05-28 04:48:18
问题 I am trying to get the correct time zone from the date picker in swift using time formatter, it's not working. I'm getting UTC, not EST. 1) If I print dateFormatter.stringFromDate(datePicker) I get EST, but 2) I don't need a string, I need an NSDate in EST so 3) I can use it to get the timeIntervalSinceDate(NSDate) in EST. My trick of trying to take it from string back to NSDate as seen below didn't work. It's still in UTC and the time interval since date is not right. dateFormatter.locale =

How to get time from YYYY-MM-dd'T'HH:mm:ss.sssZ

我与影子孤独终老i 提交于 2020-05-26 07:45:09
问题 Hi I want to get time as 11:40 from "2017-07-31T11:40:00.000Z" Below is the code I am using: let formatter = Foundation.DateFormatter() formatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ss.sssZ" //2017-04-01T18:05:00.000 let date1 = formatter.date(from: data.arvTime) print("date:\(String(describing: date1))") let dateFormatterPrint = Foundation.DateFormatter() dateFormatterPrint.dateFormat = "HH:mm" let resultTime = dateFormatterPrint.string(from: date1!) print("time:\(String(describing: resultTime)