I am trying to create an NSDate object from 2 NSStrings.
One of the NSStrings is of the form ==> mm/dd/yyyy (g.date in the following sample)
The other is of the form ==> hh:mm am/pm (g.time in the following sample)
The following code:
for(Game *g in _games) {
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd'/'MM'/'yyyy HH':'mm"];
NSString *dateString = [NSString stringWithFormat:#"%# %#", g.date, g.time];
dateString = [dateString substringToIndex:[dateString length]-3];
NSDate *date = [format dateFromString:dateString];
NSLog(#"%# ==> %#", dateString, date);
}
produces inaccurate and unpredictable output like this :
2012-04-30 19:39:06.923 MLP[27866:fb03] 05/03/2012 8:30 ==> 2012-03-05 13:30:00 +0000
2012-04-30 19:39:06.923 MLP[27866:fb03] 05/03/2012 8:45 ==> 2012-03-05 13:45:00 +0000
2012-04-30 19:39:06.924 MLP[27866:fb03] 03/29/2012 9:30 ==> (null)
2012-04-30 19:39:06.924 MLP[27866:fb03] 03/29/2012 8:15 ==> (null)
2012-04-30 19:39:06.925 MLP[27866:fb03] 03/01/2012 9:15 ==> 2012-01-03 14:15:00 +0000
2012-04-30 19:39:06.925 MLP[27866:fb03] 05/03/2012 9:00 ==> 2012-03-05 14:00:00 +0000
2012-04-30 19:39:06.926 MLP[27866:fb03] 03/29/2012 9:00 ==> (null)
2012-04-30 19:39:06.926 MLP[27866:fb03] 05/03/2012 9:15 ==> 2012-03-05 14:15:00 +0000
I am cutting off the AM/PM part of the time string because I cannot find out how to indicate it's presence in the NSDateFormatter syntax. How can I do this?
Help appreciated,
Pachun
Figured it out for anyone who may run into this in the future. This is what fixed it:
// Sort by date
- (NSComparisonResult)compare:(Game *)g {
// Parse date's strings to an NSDate
NSDateFormatter *format = [NSDateFormatter new];
[format setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *otherDateString;
NSString *myDateString;
if([g.time length]==7)
otherDateString = [NSString stringWithFormat:#"%# 0%#", g.date, g.time];
else otherDateString = [NSString stringWithFormat:#"%# %#", g.date, g.time];
if([_time length]==7)
otherDateString = [NSString stringWithFormat:#"%# 0%#", _date, _time];
else otherDateString = [NSString stringWithFormat:#"%# %#", _date, _time];
NSDate *otherDate = [format dateFromString:otherDateString];
NSDate *myDate = [format dateFromString:myDateString];
return [myDate compare:otherDate];
}
Related
If I have this date:
dtToUse __NSTaggedDate * 2017-12-31 05:00:00 UTC
which I get from a string using this method:
- (NSDate*) convertStringToDate : (NSString*) strToConvert andTheFormatToUse: (NSString*) strFormat {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:strFormat];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
return [dateFormatter dateFromString:strToConvert];
}
and I use this code to display it:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/YYYY"];
NSString* strDate = [dateFormatter stringFromDate:dtExpiration];
self.txtCurrentField.text = [dateFormatter stringFromDate:dtExpiration];
But no matter what I do, the year increments 1 - so 2017 becomes 2018
This only happens for Dec. 31, any year. I suspect it is a time zone issue but I am not sure how to fix, as I am converting time zone to local already
Just try
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
instead of
[dateFormatter setDateFormat:#"MM/dd/YYYY"];
After that, it will show 12/31/2017.
Different between YYYY and yyyy.
https://stackoverflow.com/a/15133656/1342266
I am receiving date in string form from server and I need to show the date according to my time zone (Indian: GMT +5:30).
Here is my code
NSString *dateString = #"2015-08-10 11:45:10";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//Create the date assuming the given string is in GMT
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(#"%#",date);
NSLog(#"%#",[dateFormatter stringFromDate:date]);
//Create a date string in the local timezone
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSString *localDateString = [dateFormatter stringFromDate:date];
NSLog(#"date = %#", localDateString);
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:[NSTimeZone localTimeZone].secondsFromGMT];
NSDate *date2 = [dateFormatter2 dateFromString:localDateString];
NSLog(#"%#",date);
And the Log is:
2015-08-10 11:45:10 +0000 (NSDate)
2015-08-10 11:45:10 (NSString)
date = 2015-08-10 17:15:10 (NSString)
2015-08-10 11:45:10 +0000 (NSDate)
My issue is with last log (2015-08-10 11:45:10 +0000)
Why it is not 2015-08-10 17:15:10??
The last line prints what it does because you're printing an NSDate, and NSDate does not have a time zone. It literally has no attribute or internal state that indicates a time zone. Time zones exist only for presenting data to users, commonly as you're doing via NSDateFormatter. But if you log the value of an NSDate you'll get a GMT string, since the NSDate itself has no time zone.
I have the following code:
-(void)setDate:(double)dateInterval {
NSDate *date = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)dateInterval];
NSLog(#"NSDate-Result: %#", [date description]);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
[formatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[formatter setDateFormat:#"YYYY-MM-DD HH:mm:SS"];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"String-Result: %#", dateString);
date_label.text = dateString;
}
in NSDate the Date is correct, but the converted string isn't correct anymore.
Results:
2012-08-03 10:58:40.469 iSkizzenaufmass[4148:c07] NSDate-Result:
2012-08-03 08:08:35 +0000 2012-08-03 10:58:40.471
iSkizzenaufmass[4148:c07] String-Result: 2012-08-216 10:08:48
You have to use yyyy-MM-dd HH:mm:SS instead of -DD (DD is day of the year)
EDIT: changed YYYY to yyyy as YYYY is week based year and could differ from current year (see link below for all opts)
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
I have a string which represents a date stored in military time. I want to display this string in a label in 12 hr time. Here is my code snippet:
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:timeZone];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mm:ss"];
NSLog(#"Current Date: %#", [dateFormat stringFromDate:[NSDate date]]);
NSLog(#"Sent date: %#",[myPlanData valueForKey:#"planDate"]);
NSDate *aDate =[dateFormat dateFromString:[myPlanData valueForKey:#"planDate"]];
NSLog(#"Converted date is: %#",aDate);
NSString *planDateString = [dateFormat stringFromDate:aDate];
NSLog(#"The converted date string is: %#",planDateString);
planDateLabel.text=planDateString;
The output is:
Current Date: 06/28/2012 10:08:48 - (so my date formatter appears correct?)
Sent date: 06/30/2012 20:47:34 - (this is the value being sent)
Converted date is: (null) - (Here is where it breaks!)
The converted date string is: (null)
If i change my dateformat to
[dateFormat setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
Everything goes smooth but I end up with Military time being displayed. I simply want to convet that to 12 hr time and display in a label
Well here is how i ended up fixing it
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:timeZone];
[dateFormat setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSDateFormatter *dateFormatback = [[NSDateFormatter alloc] init];
[dateFormatback setTimeZone:timeZone];
[dateFormatback setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSLog(#"Current Date: %#", [dateFormat stringFromDate:[NSDate date]]);
NSLog(#"Sent date: %#",[myPlanData valueForKey:#"planDate"]);
NSDate *aDate =[dateFormat dateFromString:[myPlanData valueForKey:#"planDate"]];
NSLog(#"Converted date is: %#",aDate);
NSString *planDateString = [dateFormatback stringFromDate:aDate];
NSLog(#"The converted date string is: %#",planDateString);
planDateLabel.text=planDateString;
Not sure if this is the best or right way to do it but it works!
I have a Date format:
2009-08-10T16:03:03Z
that I want to convert to:
#"MMM dd, HH:mm a"
I retrieve the xml format in an NSString. I tried to use the NSDateformatter:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMM dd, HH:mm a"];
any ideas?
NSString *yourXMLDate = #"2009-08-10T16:03:03Z"
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *inputDate = [inputFormatter dateFromString:yourXMLDate];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"MMM dd, HH:mm a"];
NSString *outputDate = [outputFormatter stringFromDate:inputDate];
Hope this helps!
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700
NSDate *date = [dateFormatter dateFromString:self.currentDate];
[item setObject:date forKey:#"date"];
[items addObject:[item copy]];
here edit the following code
[dateFormatter setDateFormat:#"E, d LLL yyyy HH:mm:ss Z"]; // Thu, 18 Jun 2010 04:48:09 -0700
to your required Format.