I got a date string from server side, which is Tue, 28 May 2019 13:24:06 +0000. I tried to do following:
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .full
let date = dateFormatter.date(from: "Tue, 28 May 2019 13:24:06 +0000")
result is nil
How can I convert a string like this to a Date?
You need to set a specific dateFormat. And when doing so, set the date formatter's locale to en_US_POSIX.
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
let date = dateFormatter.date(from: "Tue, 28 May 2019 13:24:06 +0000")
See the documentation for dateFormat for links that take you a full description of what all of the format specifiers mean.
Keep in mind that when converting a Date to a String for display to the user, then using date and time styles is the best solution. But for parsing a String into a Date, use dateFormat.
Related
I have a Date : 2019-07-18 00:30:32 GMT+10:00.
I want to convert string to date but its gives wrong date.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZZ"
dateFormatter.timeZone = NSTimeZone.local;
dateFormatter.locale = NSLocale.current;
let date = dateFormatter.date(from:isoDate)!
Its gives wrong date : "Jul 17, 2019 at 8:00 PM"
When working with fixed format dates, such as RFC 3339, you set the
dateFormat property to specify a format string. For most fixed
formats, you should also set the locale property to a POSIX locale
("en_US_POSIX"), and set the timeZone property to UTC.
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
Reference : Apple
I seen it shows correct date, It is based on your Time zone:
Check with :
https://www.epochconverter.com/
I have two string , date and time . date string has a date in format "MM-dd-yyyy" and time in format "hh:mm a" , I want to create a 10 digit timestamp from the same . I did the following but I am getting issue with this. Any help is appreciated.
let idate = (userInstance.userData?.Date!)! + "T" + (userInstance.userData?.Time!)! + "+0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
let date = dateFormatter.date(from: idate)!
print(date)
let timestamp = Int(date.timeIntervalSince1970)
print(timestamp)
You cannot force a date containing AM/PM time to ISO 8601. ISO 8601 dates are always represented in 24-hour mode.
Besides your order of year, month and day is not ISO 8601 compliant.
Specify the appropriate date format MM-dd-yyyyhh:mm aZ
let datePart = "09-18-2018"
let timePart = "4:22 pm"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "MM-dd-yyyyhh:mm aZ"
let date = dateFormatter.date(from: datePart + timePart + "+0000")!
let timestamp = Int(date.timeIntervalSince1970)
print(timestamp)
You are crashing here:
let date = dateFormatter.date(from: idate)!
That's because you are claiming that idate is a string in this format:
"yyyy-MM-dd'T'HH:mm:ssZ"
But it isn't. When you convert from a string to a date, your format string must exactly match the format of the string.
Then you can supply a different format and convert the date to a new string.
I would like to convert any time, UTC,, GMT+2 .. etc , anything to be only GMT +3
I tried this code but no success
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.timeZone = TimeZone.current
let currentdate = formatter.string(from: date)
print("currentdate \(currentdate)")
let gmt = DateFormatter()
gmt.timeZone = TimeZone(secondsFromGMT: 3600*3)
gmt.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let gmtDate = gmt.date(from: currentdate)!
print("gmtDate -> \(gmtDate)")
I tried abbreviations for the time zone, same result the date comes out to be only GMT.
Any ideas?
Your code has a lot of issues. First, there is no reason to go from Date to String and back to Date. Second, if you are converting a String to a Date, and the String contains its own timezone information, then setting the formatter's timeZone is pointless. The timezone in the string will be used when calculating the associated Date. There are only two cases where setting a date formatter's timezone makes sense:
When parsing a date/time string that does not contain any timezone information. The formatter's timezone will then be used to interpret the string.
When converting a Date to a String. The formatter's timezone will be used when generating the resulting string from the date.
If you simply want to show any Date as a String in a specific timezone then all you need is:
let date = Date() // some date
print("Original Date (in GMT): \(date)")
// Setup a formatter with a date and time style
let formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 3600 * 3) // the desired timezone
formatter.dateStyle = .long
formatter.timeStyle = .medium
let string = formatter.string(from: date)
print("GMT+3 style result: \(string)")
// For comparison, set the formatter to a specific format
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
let string2 = formatter.string(from: date)
print("GMT+3 format result: \(string2)")
Output (for the en_US locale):
Original Date (in GMT): 2017-10-28 20:53:59 +0000
GMT+3 style result: October 28, 2017 at 11:53:59 PM
GMT+3 format result: 2017-10-28 23:53:59 +0300
There is no need to convert any time. Simply create a String from a Date to get the desired output.
Note that, by default, a DateFormatter shows its result in local time. Set the formatter's timeZone if you want the result in some other specific timezone.
Also note that printing a Date object always shows the date in UTC time (+0000). Many people get confused by this and think they are getting the wrong date when they are not.
Swift 5
private func convertDate(string:String) -> String {
let dateFormatter = DateFormatter()
// format in which the date and time comes from the server
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
// set the time zone, the time is stored on the server
dateFormatter.timeZone = TimeZone(abbreviation: "GMT+3")
// convert to Date
let date = dateFormatter.date(from: string)
// now set our local time zone
dateFormatter.timeZone = TimeZone.current
// time format we need
dateFormatter.dateFormat = "dd.MM.yyyy HH:mm"
// convert the Date obtained above into text format
let displayString = dateFormatter.string(from: date!)
// local time is ready)))
return displayString
}
There seem to be five date styles for the DateFormatter class: none, short, medium, long, and full. However, none of these seem to return the lock screen date style, which is as follows:
Tuesday, 6 June
Using DateFormatter's .long style returns the year as well:
Tuesday, 6 June 2017
Additionally, this lock screen date style will change with the current localization/regional settings.
Is there a way to return the date, à la iOS lock screen date style (along with localizational changes)?
You can get the localized format for any combination of date components:
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.setLocalizedDateFormatFromTemplate("EEEE MMMM d")
print(dateFormatter.string(from: date)) // Friday, 4 August
In Spanish:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "es")
dateFormatter.setLocalizedDateFormatFromTemplate("EEEE MMMM d")
print(dateFormatter.string(from: date)) // viernes, 4 de agosto
Note how the order of components has been automatically changed and correct separators inserted.
You can use the custom date format "EEEE, d MMMM"
Swift 3
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, d MMMM"
dateFormatter.string(from: date)
// Friday, 4 August
I want dateString in this format "2016-12-22T08:00:00-08:00" to be converted to date object in users local time for example 2016-12-22 21:30:00 +0530 when the users time is +05:30 from UTC
OR
I want dateString in this format "2016-12-22T08:00" which is PST time to be converted to date object in users local time for example 2016-12-22 21:30:00 +0530 when the users time is +05:30 from UTC or 2016-12-22 16:00:00 +0000 when the users time is 00:00 from UTC
When I try the below code and print dateObject it prints Optional(2016-12-22 02:30:00 +0000) but I want 2016-12-22 21:30:00 +0530 since my local time is +05:30 from UTC.
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss-08:00"
dateFormatter.timeZone = NSTimeZone.local
let dateString = "2016-12-22T08:00:00-08:00"
let dateObject = dateFormatter.date(from: dateString)
print("dateObject \(dateObject1)")
Fix your dateFormat to:
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
Remove the timeZone line. It's not needed since your date string specifies its own timezone.
dateFormatter.timeZone = NSTimeZone.local // remove this line
Now your code works fine.
"But wait, the output of print(dataObject) isn't what I want"
Yes it is. Like thousands before you, you misunderstand the output of printing a Date object.
If you wish to view a Date in the local timezone, in a given format, use a DateFormatter to convert the Date into a String. You will get the current, local timezone by default.
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .long
dateFormatter.timeStyle = .long
let newString = dateFormatter.string(from: dateObject)
print(newString)
And you get just what you wanted.