I want to convert this string to NSDate,
Mon, 21 Mar 2016 10:26:45 GMT
so I set the date format to this:
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
but it returns nil.
I tried to change this format to:
E, dd MMM yyyy HH:mm:ss zzz
EEE, dd MMM yyyy hh:mm:ss zzz
EEE, d MMM yyyy HH:mm:ss zzz
but it also returns nil.
Which date format should I use?
Your format string is ok but you have to add a compatible locale to the formatter.
For example your string works well with the US locale:
let source = "Mon, 21 Mar 2016 10:26:45 GMT"
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
let result = dateFormatter.dateFromString(source)
Try this code block :
let dateFormatter = NSDateFormatter()
let dateString = "Mon, 21 Mar 2016 10:26:45 GMT"
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss zzz"
let date = dateFormatter.dateFromString(dateString)
Try escaping the comma:
E',' dd MMM yyyy HH:mm:ss zzz
You can try the following code
let dateString:String = "Mon, 21 Mar 2016 10:26:45 GMT"
let dateFormat = NSDateFormatter.init()
dateFormat.dateStyle = .FullStyle
dateFormat.dateFormat = "ccc, dd MM yyyy HH:mm:ss zzz"
let date:NSDate? = dateFormat.dateFromString(dateString)
print(dateFormat.stringFromDate(date!))
For more info please visit NSDateFormatter formatting
Related
I am using device time to get date components. this works fine for 12 hours formate but if user changes device time to 24 hours it shows wrong calculations. used the following method to convert but it always return nil.
let dateAsString = "\(Date())"
let df = DateFormatter()
df.dateFormat = "yyyy-mm-dd HH:mm:ss zzz"
let date = df.date(from: dateAsString)
df.dateFormat = "d MMM yyyy h:mm a"
let time12 = df.string(from: date!)
print(time12)
So, the core problem you're having is the fact that yyyy-mm-dd is using mm which is minutes and not using MM which is months.
So, if instead, you tried something like...
let dateAsString = "\(Date())"
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"
let date = df.date(from: dateAsString)
df.dateFormat = "d MMM yyyy h:mm a"
let time12 = df.string(from: date!)
it would result in
21 Feb 2020 6:00 PM
Now, the problem is, you could simply do
let df = DateFormatter()
df.dateFormat = "d MMM yyyy h:mm a"
df.string(from: Date())
and get the same result.
Remember, Date does not, in of itself, have a concept of "format", beyond what its "debug" output provides, so you can't change a Date's format, instead, you use a formatter to "represent" the value of the Date in some human readable form.
I have just tried below it works fine on both cases. with AM or PM or 24Format and if you remove a in formatter.dateFormat = "dd MMM yyyy HH:mm a" gives you 24 Format. Even I am using this in my code.
let someDate = Date()
let formatter = DateFormatter()
formatter.dateFormat = "dd MMM yyyy HH:mm a"
let someDateTime = formatter.string(from: someDate)
print(someDateTime)
Print:
21 Feb 2020 09:35 AM
Try this:
let date : "2021-01-20T21:40:59.416Z"
func setDate(date: String) -> String {
var interval: Double = 0
let convertedDate = date.components(separatedBy: ":")
for (index, part) in convertedDate.reversed().enumerated() {
interval += (Double(part) ?? 0) * pow(Double(60), Double(index))
}
let date = Date(timeIntervalSinceNow: interval)
let formatter = Foundation.DateFormatter()
formatter.dateFormat = "LLLL dd, HH:mm at"
return formatter.string(from: date)}
This will return a date in this format
--> "January 21, 05:30 am"
Try,
let str = "2020-02-21 06:07:21 +0000"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-mm-dd hh:mm:ss zzz"
if let date = formatter.date(from: str) {
formatter.dateFormat = "dd MMM yyyy hh:mm a"
let formattedDate = formatter.string(from: date)
print(formattedDate)
}
Swift-this answer helped me
swift - how to convert date from am/pm to 24 hour format
// posting my work here aswell.
i had device formate in 24H.
i needed in 12H.
so i just converted date object to required formate with this code snippet.
let df = DateFormatter()
df.dateFormat = "d MMM yyyy h:mm a"
let time12 = df.string(from: Date())
print(time12)
Code for playground:
var dateStr = "Fri, 03 Aug 2018 08:55:22 GMT"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.setLocalizedDateFormatFromTemplate("EEE, dd MMM yyyy HH:mm:ss z")
let date = dateFormatter.date(from: dateStr)
In the same time I use the online service - it shows that the format I chose is correct. Where is my mistake?
Small tip when you are working with DateFormatter, if it doesn't work one way, then do it the other way:
let string = dateFormatter.string(from: Date())
print("string: \(string)")
This way you'll see how your format is really interpreted.
If you do so, you'll get:
$> string: Fri, Aug 03, 2018, 12:05:09 GMT
See the extras , and the Aug 03 vs 03 Aug?
Now, your issue is that you misunderstood localizedDateFormatFromTemplate.
In fact you could remove spaces, punctuations and order.
For instance, dateFormatter.setLocalizedDateFormatFromTemplate("ddyyyyHHmmsszEMMM") gives the same output.
You just needs to give it what kind of infos you want (day, year, months, etc.), and it create the correct dateFormat corresponding in the localized version (adding then extra punctuations, etc.).
So use instead:
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss z"
Side note:
I currently live in GMT+2, so I added dateFormatter.timeZone = TimeZone(secondsFromGMT: 0) to make it work.
You have to give dateFormatter.dateFormat to convert the string to date format. From the comment you get the answer but for others i am posting a correct answer here.
var dateStr = "Fri, 03 Aug 2018 08:55:22 GMT"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss z" //Added this line.
let date = dateFormatter.date(from: dateStr)
This question already has answers here:
NSDate is 5 hours off
(4 answers)
Closed 6 years ago.
I'm having trouble converting date String to NSDate
Here is my date string:
Fri, 08 Apr 2016 16:59:35 +0300
Trying to convert to NSDate:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
let dateFromString : NSDate = dateFormatter.dateFromString("Fri, 08 Apr 2016 16:59:35 +0300")
Return NSDate is:
2016-04-08 13:59:35 +0000
Timezone is wrong. Anyone can help with that?
Try this:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss Z"
NSTimeZone.setDefaultTimeZone(NSTimeZone(forSecondsFromGMT: 0))
let dateFromString : NSDate = dateFormatter.dateFromString("Fri, 08 Apr 2016 16:59:35 +0300")!
Note that it does not account for DST.
I have an API sending date in string with universal time but when I try to convert to NSDate format it tends to return nil. I have looked in lot of places but couldn't find a way to resolve this. Below is my code that I have written. Please help me to find where am I going wrong. The string I'm trying to convert is "Sun Feb 14 23:35:40 UTC 2016". The problem causing it to return nil is with the time zone I believe.
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "EEE MMM dd hh:mm:ss yyyy"
dateFormatter.dateFromString("Sun Feb 14 23:35:40 UTC 2016")
Like this:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEE MMM dd kk:mm:ss zzz yyyy"
let d = dateFormatter.dateFromString("Sun Feb 14 23:35:40 UTC 2016")
This is probably really simple, I have tried lots of different methods but it doesnt seem to work.
I have the following date format in a text field:
Saturday, 2 January 2016 12:00
held as:
var str = "\(dobTextField.text!)"
I would like to convert the string above into the format:
YYYY-mm-dd
any help would be much appreciated!
For Swift 2
Check this :
let str = "Saturday, 2 January 2016 12:00"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, d MMMM yyyy HH:mm"
dateFormatter.timeZone = NSTimeZone(name: "UTC")
let outputA = dateFormatter.dateFromString(str)
dateFormatter.dateFormat = "yyyy-MM-dd"
println(dateFormatter.stringFromDate(outputA!))
Output :
2016-01-02