Swift string convert to SwiftDate - ios

How can I convert 12 Oct 2018 at 10:45:46 AM to SwiftDate DateInRegion?
My code returns nil:
let ds = "12 Oct 2018 at 10:45:46 AM"
let region = Region(tz: TimeZone.current,
cal: CalendarName.gregorian.calendar,
loc: Locale.init(identifier: "en"))
let format = DateFormat.custom("EEE, dd MM yyyy HH:mm:ss zzz")
var str = ds.date(format: format, fromRegion: region)

check your dateformat , is not available, the correct format is
let ds = "12 Oct 2018 at 10:45:46 AM"
let format = DateFormat.custom("dd MMM yyyy 'at' HH:mm:ss a")
for additional info you get old answered from SO.for dateformats you get detailed answer from here

Related

iOS Swift: how to convert specific date string format to Date object, when string contains time zone abbreviation?

I am getting a date string from server in the format of "March 08, 2018 16:00:00 PST" where there is a lot of whitespace between Month & Date.
My intention is to basically remove those extra whitespaces. My idea is that- I will convert the string into Date object, and then convert back to string.
How can I convert this into Date object using Date Formatter, taking timezone into consideration.
I am concerned about the "PST" here. While converting the Date to String, I will need in the format - "March 08, 2018 16:00:00 PST" i.e. PST (or whatever time zone comes in) should stay intact in the final string.
extension String {
func getDate(fromFormat format: String = "MMMM dd, yyyy HH:mm:ss zzz") -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
return dateFormatter.date(from: self)
}
}
let myDateString = "March 08, 2018 16:00:00 PST"
myDateString.getDate()
You can call with other time formats too.
Try this
let isoDate = "March 08, 2018 16:00:00 PST"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd, yyyy HH:mm:ss z"
let date = dateFormatter.date(from: isoDate)!
Try this
class func stringToDate (dateString:String, dateFormat:String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
let dateFromString = dateFormatter.date(from: dateString)
return dateFromString
}

Swift: DateFormatter not able to parse date

I've a specific time format which I want to use to generate a Date object.
This is the format:
Fri, 17 Mar 2017 08:42:00 +0100
Here is my Swift code that should create the data object:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"
let d = dateFormatter.date(from: value)
The generation of the Date object with dateFormatter.date does always return nil. But I cant see what's wrong in my formatting string.
Can anyone see my error?
I have test it and works perfectly.
Could you test this code line to identify you Locale configuration?
print(Locale.current.identifier)
I only made a little change related to the Optional result that DateFormatter method returns
import Foundation
let the_date: String = "Fri, 17 Mar 2017 08:42:00 +0100"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale.current
dateFormatter.dateFormat = "EEE, dd MMM yyyy HH:mm:ss xx"
if let d = dateFormatter.date(from: the_date)
{
print(d)
}

Date formatter returning nil value

I have this code working but now all of a sudden, it started crashing. I could not figure it out, any help would be appreciated.
let dateFormatter = NSDateFormatter()
dateFormat = "EEE, MMM d, yyyy hh:mm a"
dateFormatter.dateFormat = dateFormat
let date = dateFormatter.dateFromString("Thu, Jan 5, 2017 12:28 PM")
It is returning date as nil. Why? I want it to return proper date.
This looks like a possible duplicate question, but it is with specific details.
let dateformattor = NSDateFormatter()
dateformattor.dateFormat = "EEE, MMM d, yyyy hh:mm a"
dateformattor.timeZone = NSTimeZone.localTimeZone()
let dt = "Thu, Jan 5, 2017 12:28 PM"
let dt1 = dateformattor.dateFromString(dt as String)
print(dt1) //print date and time UTC format.
Output :
2017-01-05 06:58:00 +0000
var dateFormatter = DateFormatter()
let dateFormat = "EEE, MMM d, yyyy hh:mm a"
dateFormatter.dateFormat = dateFormat
let date = dateFormatter.date(from: "Thu, Jan 5, 2017 12:28 PM")
OUTPUT :
Every time you work with time and date formatters remember to set locale and timezone!
Device with non-english locale will fail to parse "Thu, Jan 5, 2017 12:28 PM" as "Thu" and "Jan" are obviously english specific.
So:
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
or whatever time zone and locale you want to use

Swift get Last-Modified property from NSHTTPURLResponse as NSDate

I m trying to get the last-modified property of my URLResponse as NSDate.
I followed the instructions from:
How can I convert string date to NSDate?
Convert String to NSDate in Swift
Date string to NSDate swift
From String to NSDate in Swift
but none of them worked.. I receive the date properly as a String from the URLResponse-Object in the form of "Mon, 19 Oct 2015 05:57:12 GMT"
I need to convert this String to NSDate to be able to compare it with a localDate in the form of NSDate: 2015-10-19 05:57:12 UTC
I have also tried different dateFormats but it didn't make any difference.
my current code is as follows:
//The serverDate needs to match the localDate which is a
//NSDate? with value: 2015-10-19 05:57:12 UTC
if let httpResp: NSHTTPURLResponse = response as? NSHTTPURLResponse {
let date = httpResp.allHeaderFields["Last-Modified"] as! String //EXAMPLE: "Mon, 19 Oct 2015 05:57:12 GMT"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
let serverDate = dateFormatter.dateFromString(date) as NSDate?
//conversion always fails serverDate == nil
println("ServerDate: \(serverDate)")
}
Can anyone explain why the conversion is failing? Is there any other approach I could try to convert my Date?
Swift 4, XCode 9:
let date = httpResponse.allHeaderFields["Date"] as! String
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, dd LLL yyyy HH:mm:ss zzz"
let serverDate = dateFormatter.date(from: date)
The change to 'HH' suggested by #MikeTaverne is indeed required.
I solved it...
It was a logical Problem, since I wanted to parse a String for a Date i have to specify the input dateFormat to receive an proper Date back:
Input from Server: String = "Mon, 19 Oct 2015 05:57:12 GMT"
Which is a Date in the format: "EEEE, dd LLL yyyy HH:mm:ss zzz" (http://userguide.icu-project.org/formatparse/datetime)
--> working code:
if let httpResp: NSHTTPURLResponse = response as? NSHTTPURLResponse {
//EXAMPLE: "Mon, 19 Oct 2015 05:57:12 GMT"
let date = httpResp.allHeaderFields["Last-Modified"] as! String
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, dd LLL yyyy HH:mm:ss zzz"
serverDate = dateFormatter.dateFromString(date) as NSDate?
//serverDate is now: 2015-10-19 05:57:12 UTC
println("ServerDate: \(serverDate)")
}
Like a comment said, this code works:
if let httpResp: NSHTTPURLResponse = response as? NSHTTPURLResponse {
// "Mon, 19 Oct 2015 05:57:12 GMT"
let headerDate = httpResp.allHeaderFields["Last-Modified"] as! String;
// converter
let dateFormatter = NSDateFormatter();
dateFormatter.dateFormat = "EEEE, dd LLL yyyy HH:mm:ss zzz";
// your answer
let serverDate = dateFormatter.dateFromString(headerDate) as NSDate?;
print("ServerDate: \(serverDate)")
}
Note the capital HHinstead of hh from the accepted answer.

Format internet time string (Swift 1.2)

I'm trying to format a string from internet time to something more readable.
The input I have is something like: Mon, 27 Apr 2015 20:00:00 +0000
And I'd like to format it into just: Mon, 27 Apr
I'm fairly new to Swift so I don't know the best way to do this.
something like:
let unformattedDateString = "Mon, 27 Apr 2015 20:00:00 +0000"
let dateFormatter = NSDateFormatter()
// input format
dateFormatter.dateFormat = "E, dd MMM yyyy HH:mm:ss Z"
// create NSDate from String
let date = dateFormatter.dateFromString(unformattedDateString)!
// output format
dateFormatter.dateFormat = "E, dd MMM"
// create String from NSDate
let formattedDateString = dateFormatter.stringFromDate(date)

Resources