converting String to Date swift3 ios10 - ios

why does this return a nil? detailDate is in this format:
"Wednesday, November 23, 2016 at 2:01 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy' at 'h:mm a"
dateFormatter.dateStyle = .long
let dateForCalendar = dateFormatter.date(from: detailDate!)
I believe it returns a nil because my dateFormat is not the same as what my detailDate format is, but I don't understand why it's different.

After assigning a DateFormat why are you changing the date style ?
remove,
dateFormatter.dateStyle = .long
and it will work,
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy' at 'h:mm a"
let dateForCalendar = dateFormatter.date(from: detailDate!)

let detailDate = "Wednesday, November 23, 2016 at 2:01 PM"
let dateformatter = DateFormatter()
dateformatter.dateFormat = "EEEE, MMMM dd, yyyy' at 'h:mm a"
let strDate = dateformatter.date(from: detailDate)
print(strDate!)
try this , it's work proper

Related

Convert string to particular date format getting nil in iOS

I am getting date in string format from server. So I am trying to formatting to particular format.
But it is getting nil.
"07September16 4:09 am", This I want to format like Sep 07 2016
I am doing it with following code
dateLabel.text = model().formatStringToDate(dateString: date)
func formatStringToDate(dateString: String) -> String {
let dateStr = dateString
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d yyyy"
let formattedDate = dateFormatter.date(from: dateStr)
print("formattedDate \(formattedDate)")
//I have to return formatted string here
}
Any suggestions?
That should work;
// create `Date` from date string
let dateStr = "07September16 4:09 am"
let dateFormatterGet = DateFormatter()
dateFormatterGet.locale = Locale(identifier: "en_US_POSIX")
dateFormatterGet.dateFormat = "ddMMMMyy h:mm a"
let date = dateFormatterGet.date(from: dateStr)
// convert back Date to string as Sep 07 2016
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd yyyy"
print(dateFormatterPrint.string(from: date!))
let dateString = "07September16 4:09 am"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "ddMMMyy h:mm a"
let dateObj = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "MMM dd yyyy"
print("Dateobj: \(dateFormatter.string(from: dateObj!))")
First you must get date from your string with current format like`
let dateStr = dateString
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "ddMMMMyy h:mm a"
let formattedDate = dateFormatter.date(from: dateStr)!
print("formattedDate \(formattedDate)")
After getting the date you can get date string in which format you want just change the format`
dateFormatter.dateFormat = "MMM dd YYYY"
let convertedDateString = dateFormatter.string(from: formattedDate)

Date/Time formatting Swift

I'm trying to convert following date/time: "04-26-2018 10:17 AM" to this format: "2018-04-26T10:17"
here's my code:
let myDateString = "04-26-2018 10:17 AM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy HH:mm a"
let myDate = dateFormatter.date(from: myDateString)!
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm"
let somedateString = dateFormatter.string(from: myDate)
The output for somedateString is "2018-04-26T00:17" which is incorrect.. is there something wrong in my code?
you just need correct DateFormatter this MM-dd-yyyy HH:mm a to MM-dd-yyyy hh:mm a
let myDateString = "04-26-2018 10:17 AM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy hh:mm a"
let myDate = dateFormatter.date(from: myDateString)!
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm"
let somedateString = dateFormatter.string(from: myDate)

Sometimes NSTimeZone doesn't return proper UTC value?

I have encountered with below lines of code sometimes doesn't return proper UTC value:
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
formatter.timeZone = NSTimeZone(abbreviation: "UTC")
Returns:
"2016-12-11 2:07:35 am +0000"
Instead of:
"2016-12-11 02:07:35 +0000"
What's wrong with that?
please use these code
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
formatter.timeZone = NSTimeZone.localTimeZone()
formatter.locale = NSLocale.currentLocale()
print(formatter.stringFromDate(date))

Date format gone wrong in swift

I need to set a date in medium format like: Jul 17, 2016 4:30:00 PM,
but I get 07/17/2016 4:30:00 p.m.
Any help will be highly appreciated. Thanks in advance.
this is my code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = NSDateFormatterStyle.MediumStyle
let mydate = dateFormatter.stringFromDate(NSDate())
For me, the code you are using does exactly what you need.
In case you need you can achieve this by using MMM dd, yyyy h:mm a as the format string for your formatter object.
Code follows:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy h:mm a"
let mydate = dateFormatter.stringFromDate(NSDate())
// "Jul 17, 2016 11:48 AM"
You just need to set your date formatter locale to "en_US_POSIX" and you should also set your formatter calendar to ISO8601 to get the result you were expecting:
Xcode 8 beta 2 • Swift 3
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .ISO8601)
dateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .medium
print(dateFormatter.string(from: Date())) // "Jul 17, 2016, 4:09:48 AM\n"
Xcode 7.3.1 • Swift 2.2.1
let dateFormatter = NSDateFormatter()
dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateStyle = .MediumStyle
dateFormatter.timeStyle = .MediumStyle
print(dateFormatter.stringFromDate(NSDate())) // "Jul 17, 2016, 4:12:49 AM\n"

Found nil from dateFromString when converting string to date

Here is my code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd MMM, yyyy"
let myDate = dateFormatter.dateFromString("2016-06-10")
print(myDate)
myDate is nil when I print.
If you want to convert this "2016-06-10" in to "dd MM, yyyy" then you need to write it like below code,
let dateString = "2016-06-10"
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let date = formatter.dateFromString(dateString)
formatter.dateFormat = "dd MMM, yyyy"
let st = formatter.stringFromDate(date!)
print(st) //output : 10 Jun, 2016
Hope this helps you.

Resources