Need help for specific date format - iphone

I have next date string,
2011-08-30T11:44:54.345-04:00
Help me to compose right format to parse it with dateFromString:

You need to eliminate the last colon (timezone, "-04:00" -> "-0400"), as Z will take something like "-0800", then you can give this a try:
NSString *dateString = #"2011-08-30T11:44:54.345-04:00";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-ddEHH:mm:ss.SSSZ"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat release];
For more information on date formatting, refer to this page.

This is following the tilo's answer but with few things added.
NSString *dateString = #"2011-08-30T11:44:54.345-04:00";
NSRange range = [dateString rangeOfString:#":" options:NSBackwardsSearch];
dateString = [dateString stringByReplacingOccurrencesOfString:#":" withString:#"" options:0 range:range];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-ddEHH:mm:ss.SSSZ"];
NSDate *date = [dateFormat dateFromString:dateString];
[dateFormat release];
You would get the required format.

Related

How to change the date format in iphone sdk

I'm getting Response from Webservices Date like "2012-08-17T00:00:00". I want to Display Date only like 17-08-2012. How Change Date format and Remove that time...
//String froms web service(ws)
NSString *stringFromWS =[NSString stringWithString:#"2012-08-17T00:00:00"];
//date formatter for the above string
NSDateFormatter *dateFormatterWS = [[NSDateFormatter alloc] init];
[dateFormatterWS setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date =[dateFormatterWS dateFromString:stringFromWS];
//date formatter that you want
NSDateFormatter *dateFormatterNew = [[NSDateFormatter alloc] init];
[dateFormatterNew setDateFormat:#"dd-MM-yyyy"];
NSString *stringForNewDate = [dateFormatterNew stringFromDate:date];
NSLog(#"Date %#",stringForNewDate);
also refer the link for more explanation
http://mobiledevelopertips.com/cocoa/date-formatters-examples-take-2.html
NSDate *today = [NSDate date];
NSLog(#"Date today: %#", today);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init] ;
dateFormat.dateFormat = #"dd-MM-yyyy";
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"Date: %#", dateString);
Also see the below date and time format styles
NSDateFormatterNoStyle
NSDateFormatterShortStyle
NSDateFormatterMediumStyle
NSDateFormatterLongStyle
NSDateFormatterFullStyle
You can set it like [dateFormat setDateStyle:NSDateFormatterShortStyle];

NSDateFormatter is returning null for date in ios 4.3.3

I am converting string to date and again to string. using following code
NSString *newDate =[NSString stringWithFormat:#"%#",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];
NSLog(#"New Date is %#",newDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy h:mm:ss a"];
NSDate *myDate = [dateFormat dateFromString:newDate];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSString *str = [dateFormat stringFromDate:myDate];
NSLog(#"DAte is %#",str);
It is returning date in simulator but not in iphone 4.3.3
please help
NSDateFormatter will return nil if it can't get the date from a string.
To see if it can parse the date add an other NSLog statement:
NSString *newDate =[NSString stringWithFormat:#"%#",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];
NSLog(#"New Date is %#",newDate);
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy h:mm:ss a"];
NSDate *myDate = [dateFormat dateFromString:newDate];
NSLog(#"Date parsed: %#", myDate);
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSString *str = [dateFormat stringFromDate:myDate];
NSLog(#"DAte is %#",str);
But since you only want the firs part of the string why not just substring it.
NSString *str = [newDate substringToIndex:10];
and what is returned here:
NSString *newDate =[NSString stringWithFormat:#"%#",[(Jobs *)[tableArray objectAtIndex:indexPath.row]Date]];
it the property Date already a NSDate ?, can you show your .h file for Jobs?

String to a Format yyyy-MM-dd HH:mm:ss Iphone

I have an nsstring ,see below
NSString *Mydate=#"9/8/2011"; in month/day/year format.
I want this string to be in the format yyyy-MM-dd HH:mm:ss.
for eg: 2011-09-08 15:51:57
so that i need to show the string in a label in the later format.
Thanks all.
Try below code with valid input string that will help you.
NSString *dateStr = #"9/8/2011 11:10:9";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:#"d/M/yyyy hh:mm:s"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:s"];
NSString *st = [dateFormat stringFromDate:d];
NSLog(#"%#",st);
[dtF release];
[dateFormat release];
You may use NSDateFormatter

How to specify the NSDate Format in Iphone app?

I am trying to make a date format in such a manner hours.minutes day/month/year
I have uses this code :
NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
NSDate *dateAndtime = [NSDate date];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterShortStyle];
//[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString* dateforBD=[[NSString alloc]init];
dateforBD =[formatter stringFromDate:dateAndtime];
But this code not give me my format.
Also i have tried this code:
NSLocale *locale = [NSLocale currentLocale];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:#"E MMM d yyyy"
options:0 locale:locale];
[formatter setDateFormat:dateFormat];
[formatter setLocale:locale];
It's also give me different format. How can i achieve the required format means 13.10 21/4/2011
All the formats return in month before date. May i have used wrong way for this ?
Thanks in advance.
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];
To explore more visit http://www.stepcase.com/blog/2008/12/02/format-string-for-the-iphone-nsdateformatter/
Hope it helps.
for date before month use -
[dateFormat setDateFormat:#"dd/MM/yyyy hh:mm"];
As specified in Apple's NSDateFormatter reference, the NSDateFormatter uses Unicode Date Format Patterns.
According to the unicode patterns, you would need the following pattern to achieve "13.10 21/4/2011":
hh:mm dd/MM/yyyy
So you would use this code:
NSDate *dateAndtime = [NSDate date];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"hh:mm dd/MM/yyyy"];
NSString *newlyFormattedDateString = [dateFormatter stringFromDate:dateAndtime];

How to handle different date time formats using NSDateFormatter

I have a String with a datetime format: "YYYY-MM-DD HH:MM:SS".
I use this in my source code:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"%e. %B %Y"];
NSString *test = [formatter stringFromDate:#"2010-01-10 13:55:15"];
I want to convert from "2010-01-10 13:55:15" to "10. January 2010".
But my implementation does not work.
What's wrong here?
Thanks a lot in advance & Best Regards.
Updated source code:
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"%Y-%m-%d %H:%M:%S"];
NSString *test1 = [formatter stringFromDate:#"2010-01-10 13:55:15"];
NSDateFormatter *formatter1 = [[[NSDateFormatter alloc] init] autorelease];
[formatter1 setDateFormat:#"%d. %M4 %Y"];
NSString *test2 = [formatter1 stringFromDate:test1];
A date formatter can only handle one format at a time. You need to take this approach:
NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [f dateFromString:#"2010-01-10 13:55:15"];
NSDateFormatter *f2 = [[NSDateFormatter alloc] init];
[f2 setDateFormat:#"d. MMMM YYYY"];
NSString *s = [f2 stringFromDate:date];
s will now be "10. January 2010"
Here are a few examples of working with data formatters from my code. You should be able to take any one of these functions and tweak it for your format.
USAGE
NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [Constants getTitleDateFormatter];
NSString *dateString = [dateFormatter stringFromDate:today];
[dateFormatter release];
FUNCTIONS
+ (NSDateFormatter *) getDateFormatterWithTimeZone {
//Returns the following information in the format of the locale:
//YYYY-MM-dd HH:mm:ss z (Z is time zone)
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
return dateFormatter;
}
+ (NSDateFormatter *)dateFormatterWithoutYear {
NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *format = [dateFormatter dateFormat];
format = [format stringByReplacingOccurrencesOfString:#"/yy" withString:#""];
NSRange secondSpace;
secondSpace.location = format.length-2;
secondSpace.length = 1;
format = [format stringByReplacingCharactersInRange:secondSpace withString:#""];
[dateFormatter setDateFormat:format];
return dateFormatter;
}
+ (NSDateFormatter *) dateFormatterMonthDayOnly {
NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *format = [dateFormatter dateFormat];
format = [format stringByReplacingOccurrencesOfString:#"/yy" withString:#""];
NSRange range;
range.location = 0;
range.length = 3;
format = [format substringWithRange:range];
[dateFormatter setDateFormat:format];
return dateFormatter;
}
+ (NSDateFormatter *) getTitleDateFormatter {
//Returns the following information in the format of the locale:
//MM-dd-yyyy hh:mm:ssa
NSDateFormatter *dateFormatter = [Constants getDateFormatterWithTimeZone];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *format = [dateFormatter dateFormat];
NSRange secondSpace;
secondSpace.location = format.length-2;
secondSpace.length = 1;
format = [format stringByReplacingOccurrencesOfString:#"/" withString:#"-"];
format = [format stringByReplacingCharactersInRange:secondSpace withString:#""];
[dateFormatter setDateFormat:format];
return dateFormatter;
}
First off, make sure you set the behavior to 10.4 - more modern, works better in my experience.
[dateTimeFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
Next, you can't use the same format to parse and format if they have 2 different string representations, so use 2 formatters, or change the string format between parsing and then formatting.
Also make sure you consider the formatting options:
http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1
lowercase is e is the day of week, lowercase d is the day of the month.
For month, use MMMM, not B.
You want to use [NSFormatter dateFromString:] to convert your string-based date to an NSDate instance. From there you want to use stringFromDate with the NSDate, not the string as you have written above. I'm not sure about using the same NSDateFormatter for both parsing and formatting - you may need two separate instances to handle the different format styles.