NSDate from String returning nil - ios

I am storing dates as strings in the format: 2018-07-10 21:00:29 +0000
I created a formatter to convert the strings back into dates:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ss.SSSZ";
[formatter setLocale:[NSLocale currentLocale]];
NSDate *date = [formatter dateFromString:[chore getDate]];
However, the date that is returned is always nil. Does anyone know what I'm doing wrong?

You set the wrong format in the dateformat. So it will always return the nil value.
The following method can return the date in the NSDate format.
-(NSDate *)convertDate :(NSString *)date1 //the argument date1 is the string date you used
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss Z"; // the format which you used in the string date
[formatter setLocale:[NSLocale currentLocale]]; //This for set the Locale .It is not compulsory.
NSDate *date = [formatter dateFromString:date1];
return date;
}

Refer to date formatter date formats here:
You need to update your date format to something like:
"yyyy-MM-dd HH:mm:ss Z"

Related

NSDate set format to MM/YYYY but result shows dd/YYYY

I have a sample code as below and I would like to show in MM/yyyy. However, I get result in dd/yyyy.
NSString *strJoinedDate = [prefs stringForKey:#"creation_date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss a"];
NSDate *JoinedDate = [dateFormatter dateFromString:strJoinedDate];
[dateFormatter setDateFormat:#"MM/yyyy"];
NSString *strFinalJoinedDate =[NSString stringWithFormat:#"Since %#",[dateFormatter stringFromDate:JoinedDate]];
You have the wrong date format for a date string such as 5/29/2018 3:23:28 AM. Change:
dd/MM/yyyy HH:mm:ss a
to:
MM/dd/yyyy hh:mm:ss a
You also need to set the date formatter's locale to en_US_POSIX.
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];

Converting Date string with date format as "2015-11-09 06:54:00 UTC" to NSDate

The string I get from server is in the format like "2015-11-09 06:54:00 UTC" I want to convert
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
NSDate* date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"dd MMM yyyy hh:mm a"];
NSString* formattedDate = [dateFormatter stringFromDate:date];
I tried with date formatter to be dateFormatter.dateFormat = #"yyyy-MM-dd'T'HH:mm:ssZ";
Still I get the formatted date as nil.
I don't understand how to convert this string to a proper NSDate. Please suggest.
Try the below code snippet;
NSString *dateString = #"2015-11-09 06:54:00 UTC";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss Z";
NSDate* date = [dateFormatter dateFromString:dateString];
dateFormatter setDateFormat:#"dd MMM yyyy hh:mm a"];
NSString* formattedDate = [dateFormatter stringFromDate:date];
NSLog(#"Formatted Date: %#",formattedDate);
I could say "try the code below", but that is useless.
Look at your formatting string. It says what the date formatter expects in the string.
Look at your date string.
Compare them. They don't match. The 'T' in the formatting string isn't some kind of black magic. It means "I expect a letter T in the date string". There is no letter T in the date string, that's why it doesn't work.
Read Apple's documentation carefully, and take note of the bit where it refers to the Unicode standard. And read that document at www.unicode.org carefully - There is a HUGE difference between yyyy and YYYY, and between MM and MMM, and between HH and hh, and if you don't read this and learn this, you cannot write code that gives the desired result.

iOS Dateformatter Timezone

Im trying to convert a String without timezone value like "31.05.2015" to NSDate.
Whats wrong with my DateFormatter? It returns the date 2015-05-31 22:00:00 +0000
and when I try to print it with the same formatter it prints: 01.06.2015
-(NSDateFormatter*) dateFormatter{
if(!_dateFormatter){
_dateFormatter = [[NSDateFormatter alloc]init];
[_dateAndTimeFormatter setDateFormat:#"dd.MM.yyyy"];
[_dateAndTimeFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"CEST"]];
_dateFormatter.dateStyle = NSDateFormatterShortStyle;
_dateFormatter.timeStyle = NSDateFormatterNoStyle;
}
return _dateFormatter;
}
EDIT
I updated it to
-(NSDateFormatter*) dateAndTimeFormatter{
if(!_dateAndTimeFormatter){
_dateAndTimeFormatter = [[NSDateFormatter alloc]init];
[_dateAndTimeFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS Z"];
_dateAndTimeFormatter.dateStyle = NSDateFormatterShortStyle;
_dateAndTimeFormatter.timeStyle = NSDateFormatterShortStyle;
}
return _dateAndTimeFormatter;
}
and call it with
NSDate *startDate = [self.dateFormatter dateFromString:#"31.05.2015"];
self.beginningValueLabel.text = [self.dateFormatter stringFromDate:startDate];
The result still is 01.06.2015.
startDate is 2015-05-31 22:00:00 +0000
When you use [NSDateFormatter setTimeZone:] the formatter will adjust the date for display based on that new timezone, so it's adding the timezone offset to the date before formatting it for display.
The NSDateFormatter defaults to the timezone of the device, so it would have had the same effect as setting the timezone manually. Tell the DateFormatter to use the same timezone as the date (+0000).
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0]

Converting time string to Date format iOS

I have a string #"21:57" and I want to save it with the same HH:mm format in NSDate. I tried converting it using:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.dateFormat = #"HH:mm";
NSDate *date = [dateFormatter dateFromString:stringToConvertFrom];
but I'm getting 2000-01-01 21:57:00 +0000 instead of 21:57.

Weird converting output from NSString to NSDate

I have weird result trying to output NSDate object from NSString.
My NSString is: 1976-06-11
My method to convert is:
-(NSDate*)dateFromString:(NSString *)dateString{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dateString];
return date;
}
But it output 1976-06-10 21:00:00 +0000
How could that happen? Difference in 1 day.
You have date in UTC format. Use this code to converting your date to local time:
NSTimeInterval seconds; // assume this exists
NSDate *ts_utc = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter *utcFormatter = [[NSDateFormatter alloc] init];
utcFormatter.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
utcFormatter.dateFormat = #"yyyy.MM.dd G 'at' HH:mm:ss zzz";
NSDateFormatter *localFormatter = [[NSDateFormatter alloc] init];
localFormatter.timeZone = [NSTimeZone timeZoneWithName:#"EST"];
localFormatter.dateFormat = #"yyyy.MM.dd G 'at' HH:mm:ss zzz";
NSString *utcDateString = [utcFormatter stringFromDate:ts_utc];
NSString *LocalDateString = [localFormatter stringFromDate:ts_utc];
Or you can use [NSTimeZone defaultTimeZone] to prevent hardcoded strings for timezone names. This method returns the system time zone, If no default time zone has been set.
You can use following methods to convert an UTC date string into UTC date and local date
- (NSDate *)convertIntoGMTZoneDate:(NSString *)dateString
{
NSDateFormatter *gmtFormatter = [[NSDateFormatter alloc]init];
[gmtFormatter setDateStyle:NSDateFormatterFullStyle];
[gmtFormatter setTimeStyle:NSDateFormatterFullStyle];
[gmtFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
return [gmtFormatter dateFromString:dateString];
}
- (NSDate *)convertIntoSystemZoneDate:(NSString *)dateString
{
NSDateFormatter *systemZoneFormatter = [[NSDateFormatter alloc]init];
[systemZoneFormatter setDateStyle:NSDateFormatterFullStyle];
[systemZoneFormatter setTimeStyle:NSDateFormatterFullStyle];
[systemZoneFormatter setTimeZone:[NSTimeZone systemTimeZone]];
return [systemZoneFormatter dateFromString:dateString];
}
func dateFromString(dateString: String) -> NSDate {
// Convert string to date object
var dateFormat = NSDateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd"
dateFormat.timeZone = NSTimeZone(name: "UTC")
var date = dateFormat.dateFromString(dateString)!
print(date)
return date
}
output :
1976-06-11 00:00:00 +0000
If you debug code, it shows 1 day difference but after run you will find the actual date which is you enter.
It works for me.I think it will helps you.
Thank you

Resources