NSDate current date returning nil - ios

In some scenario, I am getting currentDateString as nil.My app is in live so i can't find out exact scenario.
NSDate *currentDate = [NSDate date];
NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];

I tried this code. I got correct results. Please set DateFormatter Locale
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];

Related

Change date format using NSDateFormatter

I want to use date format like this 2015-08-14 but unfortunately I am getting this format 16/11/15
Below is the code I have used to get desired output.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle];
NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
Can anyone please suggest how to fix this issue?
Just remove the line [dateFormatter setDateStyle:NSDateFormatterShortStyle];
-(NSString*)stringFromDateWithFormat:(NSString*)formatString{
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
dateFormatter.dateFormat = formatString;
return [dateFormatter stringFromDate:self];
}
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"YYYY-MM-dd"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSLog(#"date is %#",dateString);
NSDate *selectedDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *formattedDateString = [dateFormatter stringFromDate:selectedDate];
NSLog(#"formattedDateString = %#", formattedDateString); // Prints 2015-10-15

How to convert datetime string into NSDate in iOS?

I have datetime format like this
2015-10-06 12:39:12
when i try to convert into nsdate it return me null. i am using below code for it.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [formatter dateFromString:user_post.modification_date];
Please tell me how can i convert it into correct NSDate?
dateString is your date in string format.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
Try to set like this
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
Change this line only.
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Z is the timeZone
I suggest you to set the timezone, can be the cause of the problem
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];

nil returned from dateFromString NSDateFormatter

I am trying to create a NSDate from some strings of dates I have.
Here is what I am doing.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSDate *now = [NSDate date];
NSDate *deliveryETA = [dateFormatter dateFromString:deliveryDate];
NSDate *createDate = [dateFormatter dateFromString:shipDate];
deliveryETA is always nil with this string 2014-12-23T23:59:59 but createDate is always properly initialized.
Any idea why this is?
Change NSDateFormatter for deliveryETA Like
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
// chanegn this line
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *now = [NSDate date];
NSDate *deliveryETA = [dateFormatter dateFromString:deliveryDate];
// add this line
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSDate *createDate = [dateFormatter dateFromString:shipDate];
tell me what happen...
NSDate *deliveryETA = [dateFormatter dateFromString:deliveryDate];
for deliveryETA to have the value the input string MUST contain the format yyyy-MM-dd'T'HH:mm:ssZZZZZ

NSDateFormatter locale is not working

I am using the following code to get the NSString from date
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterLongStyle;
df.timeStyle = kCFDateFormatterNoStyle;
df.doesRelativeDateFormatting = YES;
[df setLocale:[NSLocale currentLocale]];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString=[df stringFromDate:myDate];
But it displaying like 'Today','Yesterday' even after changing Locale.I want to display the localized language of that also ?
I used your code and created this:
NSDate *yesterday;
NSTimeInterval interval;
NSCalendar *cal = [NSCalendar currentCalendar];
[cal rangeOfUnit:NSDayCalendarUnit
startDate:&yesterday
interval:&interval
forDate:[NSDate date]];
yesterday = [yesterday dateByAddingTimeInterval:-interval];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = kCFDateFormatterLongStyle;
df.timeStyle = kCFDateFormatterNoStyle;
df.doesRelativeDateFormatting = YES;
[#[[NSLocale localeWithLocaleIdentifier:#"de_DE"], [NSLocale localeWithLocaleIdentifier:#"es_ES"],[NSLocale localeWithLocaleIdentifier:#"en_US"]] enumerateObjectsUsingBlock:^(NSLocale *locale, NSUInteger idx, BOOL *stop) {
[df setLocale:locale];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSString *dateString=[df stringFromDate:yesterday];
NSLog(#"%#", dateString);
}];
It outputs correctly
Gestern
ayer
Yesterday
Your problem must lay else-where.
Tested within a command-line program
Make sure that after you changed the language on device/simulator you kill the app completely before you open it again.
Hi I write this Function for LocalTime it Takes input of date as String and covert to Local Time String. May it help you
+(NSString*)LocalTimeWitheDateString:(NSString*)dateString{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
NSTimeZone *timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date =[formatter dateFromString:dateString];
NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:localTimeZone];
NSString *LnewTimeZoneDateString = [formatter stringFromDate:date];
return LnewTimeZoneDateString;
}

invalid date in NSDateFormatter

self.dateFormatter = [[NSDateFormatter alloc] init];
[self.dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[self.dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[self.dateFormatter setDateFormat:#"yyyyMMdd"];
NSDate *date = [NSDate date];
NSString *dateString = [self.dateFormatter stringFromDate:date];
this code sometimes returns for me incorrect date such as 201300090005.
I don't understand why.
I tried the something like this, but it always works fine for me:-
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init]autorelease];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"yyyyMMdd"];
NSDate *date = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"yourDateString=%#",dateString);

Resources