This extremely simple task was apparantly very hard...
NSDate *date;
date = [someMethod here];
//I've checked with debugger that the method returns an object of type NSDate.
//The description of date at this point is: "2012-02-02 19:42:00 +0000"
NSDateFormatter *dateFormat;
[dateFormat setDateFormat:#"dd-MM-yy hh:mm"];
NSString *dateString = [dateFormat stringFromDate:date];
dateString is just NIL
Does anyone know what's wrong?
Edit: What i really want to achieve is simply:
NSString *receivedDate = #"2012-02-02T20:42:00+01:00";
NSString *fixedDate = [do some magic]
//value of fixedDate is now: "02-02-12 20:42"
As Anna said you need to allocate an instance of NSDateFormatter because all you get is a NULL ptr, which just ignores the messages setDateFormat and stringFromDate, leaving you with NULL.
But also your format is for hours is incorrect. Refer to Date Formatter reference
This works for me:
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:##"dd-MM-yy HH:mm"];
NSString *dateString = [df stringFromDate:date];
Related
I am receiving a date/time as a NSString from my server where I am converting that time into a NSDate to the users local time using NSTimeZone. After which I try to reformat this NSDate into a better more readable NSString using the new NSDateFormatter format, however when I try to apply this new format it reverts the resulting dateString back to the original Server time.
I would like to know what I am doing wrong, I would like to show the converted time in the new format.
this is the code I am using
// set date format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
// change time to systemTimeZone
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:timeZone];
NSDate *localTime = [dateFormatter dateFromString:[singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]];
// reformat converted Time to readable format
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"dd/MM/yy - hh:mm a"];
NSString *dateWithNewFormat = [dateFormat1 stringFromDate:localTime];
NSLog(#"TimeZone - %#", timeZone);
NSLog(#"UTC ServerTime - %#", [singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]);
NSLog(#"UTC to deviceTimeZone - %#", localTime);
NSLog(#"NewFormat - %#", dateWithNewFormat);
This is an example of my output
TimeZone - Pacific/Auckland (NZST) offset 43200
UTC ServerTime - 2013-08-22 01:45:59
UTC to deviceTimeZone - 2013-08-21 13:45:59 +0000
NewFormat - 22/08/13 - 01:45 AM
any help would be greatly appreciated
The NSDateFormatter that reads the date must be set to the timezone that the date you are parsing is in, in your case, it is UTC. The date formatter will then be able to produce an NSDate object (which represents a specific moment in time regardless of timezones). You can then give that NSDate object to another NSDateFormatter that is configured to format dates in a specific time zone.
// set date format
NSDateFormatter *dateParser = [[NSDateFormatter alloc] init];
dateParser.dateFormat = #"yyyy-MM-dd HH:mm:ss";
dateParser.timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
NSDate *specificMomentInTime = [dateParser dateFromString:[singleInstanceActivationHistoryDictionay objectForKey:#"ActivationTime"]];
// reformat converted Time to readable format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"dd/MM/yy - hh:mm a";
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:specificMomentInTime];
NSLog(#"UTC ServerTime - %#", specificMomentInTime);
NSLog(#"NewFormat - %#", dateWithNewFormat);
I am getting NSString 2012-08-17T10:56:45.508205 as time from Django API.
I am trying to convert that string into NSDate object with this code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *date = [dateFormatter dateFromString:stringToConvert];
But date object is null. Where am I going wrong?
The ISO8601 string you're getting from the Django API (2012-08-17T10:56:45.508205) does not include a time zone component. However, you are including that field symbol (Z) in your format string. Just remove that and it should work fine.
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
See the Unicode Technical Reference for more information on the formatting codes:
how to get only date from datetime date value
my code is:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
[dateFormat setDateStyle:NSDateFormatterShortStyle];
lblDateAvailable.text = [dateFormat stringFromDate:[datepick date]];
NSDate *date=[dateFormat dateFromString:lblDateAvailable.text];
NSLog(#"%#",date);
result=2012-08-17 18:30:00 +0000
i want to get only date from string.so plz suggest me.
Convert Date into string and using this method Separated the component and pass the Separated value in your label.
NSArray* components = [yourdatestring componentsSeparatedByString:#" "];
I'm trying to produce a string using a NSDate category in this way:
NSString* dateString = nil;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocal currentLocale]];
[dateFormat setDateFormat:#"dd LLL YYYY"];
dateString = [dateFormat stringFromDate:self];
return dateString;
The conversion works fine except in ONE case (I report the debug session):
if I try to convert an NSDate object like this:
(gdb) po self
2012-01-01 00:00:00 +0000
I obtain:
(gdb) po dateString
01 Jan 2011
Why the year is set back to 2011????
PS. I have already checked NSDate returns wrong year and I'm NOT using the Japanese calendar.
thanks a lot
Try this:
NSDate *pickerDate = [NSDate date];
NSCalendar* calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents* components = [[[NSDateComponents alloc] init] autorelease];
components.day = 0; //This value to take from today to next 1 or 2 or 3 days
NSDate* newDate = [calendar dateByAddingComponents: components toDate: pickerDate options: 0];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMMM"];
NSString *textDate = [NSString stringWithFormat:#"%#",[dateFormatter stringFromDate:newDate]];
[dateFormatter release];
I have a JSON feed coming into my app, one of the fields is a combined date & time string which I need to split into discrete date and time strings for display in a table cell. An example of input from the JSON is:
2012-01-18 14:18:00.
I'm getting a bit confused with the date formatter, and clearly I'm not doing it right - I've tried a number of tutorials but most just seem to show how to format a date.
I've tried something a little like this to get just the time:
NSDictionary *rowData = [self.raceData objectAtIndex:[indexPath row]];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"h:mma"];
NSDate *raceDate = [dateFormat dateFromString:[rowData valueForKey:#"race_time"]];
NSString *raceTime = [dateFormat stringFromDate:raceDate];
but on output raceTime is just null.
Any help appreciated.
maybe the format should be more like
[dateFormatter setDateFormat:#"yyyy-MM-dd 'at' HH:mm"];
have a look at http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
might clear things up abit
Right, I have this working - it's probably a bit messy but here's what I did:
NSDictionary *rowData = [self.raceData objectAtIndex:[indexPath row]];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate* raceDate = nil;
NSError* dateError = nil;
NSRange dateRange = NSMakeRange(0, [[rowData valueForKey:#"race_time"] length]);
[dateFormat getObjectValue:&raceDate forString:[rowData valueForKey:#"race_time"] range:&dateRange error:&dateError];
[dateFormat setDateFormat:#"HH.mm"];
NSString *raceTime = [dateFormat stringFromDate:raceDate];
I can now output raceTime as a standalone time. I had to use getObjectValue:forString:range:error: to parse the original string to a date before changing the formatting and parsing it again.
As I'm using this in a table I suspect I'll need to use a static formatter so it doesn't slow everything down - if anyone can give a best practice on doing that I'd appreciate it.
If you are sure that the input string format wouldn't change – you might use something similar to:
NSString *date = nil;
NSString *time = nil;
NSDictionary *rowData = [self.raceData objectAtIndex:[indexPath row]];
NSString *raceTime = [rowData valueForKey:#"race_time"];
NSArray *dateParts = [raceTime componentsSeparatedByString:#" "];
if ([dateParts count] == 2) {
date = [dateParts objectAtIndex:0];
time = [dateParts objectAtIndex:1];
}