NSDateFormatter always returning nil - ios

This is my code:
var sdArr = sd.componentsSeparatedByString(".")
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'hh:mm:ss"
let date = dateFormatter.dateFromString(sdArr[0])
The value of sdArr[0] is "2015-10-01T14:30:00"
Why is the value of date is nil? This was working fine 2 days ago and I didn't touch my code. Now suddenly out of the blue value of date is nil.
I've tried putting the date string in place of sdArr[0] but it still not working

Please use date formatter as
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
You are using hh which is for 12 hour format, HH is for 24 hour format

add dateformat yyyy-MM-dd'T'hh:mm:ss to yyyy-MM-dd'T'HH:mm:ss because you are getting hour in 24 hour format not 12 hour format so you have to use HH
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss" //HH
Please refer Formatting date and time with iPhone SDK? (Vanya's answer) to know more regarding date formaters.

Use this NSDateFormatter
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"

Related

Converting String to Date is always 2 hours behind

I know this is a topic with several answers and whilst maybe i missed something similar to mine, in all my effort i've looked but can't find the same thing:
I have a string of date format:
24/12/2019 10:34 AM
I am trying to convert it to Date type, but this is what i get when i print the converted date:
2019-12-24 08:34:00 +0000
It is ALWAYS 2 hours behind.
I have tried:
dateFormatter.timeZone = TimeZone(abbreviation: "CAT")
and
dateFormatter.timeZone = .current
to absolutely no avail. Also, the printed date is not formatted according to my required format
dd/MM/yyyy hh:mm a
My code is as below:
var dateFormat = "dd/MM/yyyy hh:mm a"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "CAT")
dateFormatter.dateFormat = dateFormat
let date = dateFormatter.date(from: "24/12/2019 10:34 AM")
print(date)
Any assistance would be appreciated

Date conversion from 24 hours format to 12 hours format in IOS

My iPhone device's default time format is 24 hours. I am receiving a date string in the app which is also in 24 hours format. But when I am converting this date string to 12 hours format date string using DateFormatter then it is still returning the 24 hours format.
I am receiving the date in following format "yyyy-MM-dd HH:mm:ss" and I am converting it to "MM/dd/yyyy hh:mm a" but the time is still in 24 hours format.
For e.g. Received date is: 2019-01-08 14:01:04 and converted date is 01/08/2019 14:01.
format = MM/dd/yyyy hh:mm a
timeZone = Asia/Kolkata (current)
locale = Locale.current
here is my code snippet:-
let formatter = DateFormatter()
formatter.dateFormat = format
formatter.timeZone = timeZone
formatter.locale = locale
formatter.string(from: DATE_OBJECT)
I am able to found the solution.
Set the local of your NSDateFormatter to en_US_POSIX will fix this. It works for both 24-hour and 12 hour format.
Locale.init(identifier: "en_US_POSIX")
Here is the link for detailed information: http://stephentolton.com/nsdateformatter-24-hour-time-and-region-formats/

How to compare 2 dates and get how far they are apart from each other

I have this string date value
let stringDate = 2018-06-10T13:57:23.232+0000
the string date format will always be the same and I want to have output similar to one of these
6 year, 3 month, 1 day
or
3 month, 20 days
or
1 day
Basically I'm comparing the stringDate with current date and display how far they are apart from each other.
I'm able to get the date from string using this
let dateFormatter = DateFormatter()
var dateFromString: Date?
dateFormatter.timeZone = NSTimeZone.local
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
if let dateReturn = dateFormatter.date(from: stringDate) {
dateFromString = dateReturn
}
but I'm stuck on how to get the custom input like above. Thank you for helping me

Cannot convert to NSDate from this specific date (1994-04-01) String

Something strange happen to me.
I'm not able to convert this specific date (1994-04-01) String.
Can anyone check this and let me know if it reproduce in your code?
Steps to Reproduce:
Swift:-
let dateString = "1994-04-01"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateFromString = dateFormatter.date(from: dateString)
Obj c:-
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString *birthdayStr = #"1994-04-01";
NSDate *birthday = [formatter dateFromString:birthdayStr];
Expected Results:
birthday = 1994-03-31 21:00:00 +0000
In UTC
Actual Results:
birthday = nil
Version:
Xcode ver :- Version 8.1 (8B62)
OS X ver :- 10.12.1 (16B2555)
you can try any different date then (1994-04-01) and it will work fine.
Test code:
import Foundation
for timeZoneId in TimeZone.knownTimeZoneIdentifiers {
let dateString = "1994-04-01"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: timeZoneId)
dateFormatter.dateFormat = "yyyy-MM-dd"
if dateFormatter.date(from: dateString) == nil {
print(timeZoneId)
}
}
Output:
Asia/Amman
Asia/Damascus
Asia/Gaza
Asia/Hebron
Asia/Jerusalem
I conclude your system time zone is set to one of the ones printed. If I type “jerusalem time zone 1994” into Google, the first result tells me that Daylight Saving Time started at midnight on April 1 in 1994. This means there was no midnight. The first instant of April 1, 1994 was in fact 1 AM in that time zone.
A DateFormatter uses a time of day of midnight by default when not parsing a time of day from the string. This makes it fail when midnight doesn't exist on the date in the string.
The solution is to not use midnight as your default time of day. Noon is a much safer default time. So, one solution is to include the time of day in the input and parse it in the format:
let dateString = "1994-04-01 12:00:00"
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: timeZoneId)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
Another solution is to give the date formatter a default date that is noon of some day:
// Reference date was 00:00:00 UTC on 1 January 2001. This is noon of the same day in UTC.
dateFormatter.defaultDate = Date(timeIntervalSinceReferenceDate: 12*60*60)
If you are going to do any date parsing or manipulation on iOS or macOS, it would be a very good idea to watch WWDC 2013 Session 227: Solutions to Common Date and Time Challenges.

Convert 12 hour date format into 24 hour format & vice versa in swift?

I have a date from a UIControl. I get date as string such as 06-12-2016 01:25 PM. Now I want to convert it into a format as 2016-12-06 13:25:00 I have tried below code t do so but it just gives me wrong date for ex. 2016-12-06 18:55:00. I have used below code for this:
func converStingToDate(str_date:String)->String
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy H:mm a"
let date = dateFormatter.date(from: str_date)
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
return dateFormatter.string(from: date!)
}
Please suggest me a better code. Also if you guys can help me to understand all this date formatting concept how it is done ?
The code is correct and the date is correct (although you should use h for 12-hour hour).
Your time zone is UTC+0530 and print() displays the date in UTC.
If you need to print the correct local time set the time zone of the formatter to UTC:
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")`

Resources