I am having a date as 2010-08-02 which is 2nd August 2010 now i want to dispaly this as Monday,8 August ,2010
Thanks for the help
You should take a look at NSDateFormatter and if neccessary create your own formatter from a String like in the second link (The last listing).
http://developer.apple.com/iphone/library/documentation/cocoa/reference/foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
http://developer.apple.com/iphone/library/documentation/cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"EEEE',' d MMMM yyyy"];
NSDate *date = [inputFormatter dateFromString:#"2010-08-02"];
NSString *dateString = [outputFormatter stringFromDate:date];
NSLog(#"%#", dateString);
[inputFormatter release];
[outputFormatter release];
Related
I have a NSString that keeps date:
1900-01-01T11:00:00
I need to format it, so I have a formatter:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
How to format this string now?
NSString* formatedDateString = [df ???];
Create a date formatter, that returns a date object for for the given string, create a second date formatter, that returns a string in the desired format from that date.
NSDateformatter *inputFormatter = [[NSDateformatter alloc] init];
[inputFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date = [inputFormatter dateFromString:dateString];
NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[outputFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
NSString *outputString = [outputFormatter stringFromDate:date];
But you could also let the output date formatter decide the style in respect to the locale:
NSDateformatter *outputFormatter = [[NSDateformatter alloc] init];
[outputFormatter setDateStyle:NSDateFormatterShortStyle];
[outputFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *outputString = [outputFormatter stringFromDate:date];
for a american locale, you should now cat the desired format.
if you want to enforce format "dd/MM/yyyy hh:mm:ss a" for the user, you should also set the locale of the output date formatter.
a complete command line example.
int main(int argc, const char * argv[])
{
#autoreleasepool {
NSString *dateString1 = #"2012-12-31T11:00:00";
NSString *dateString2 = #"2012-12-31T14:00:00";
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[inputFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
[outputFormatter setDateFormat:#"dd/MM/yyyy hh:mm:ss a"];
NSDate *date1 = [inputFormatter dateFromString:dateString1];
NSDate *date2 = [inputFormatter dateFromString:dateString2];
NSString *outputString1 = [outputFormatter stringFromDate:date1];
NSString *outputString2 = [outputFormatter stringFromDate:date2];
NSLog(#"%#", outputString1);
NSLog(#"%#", outputString2);
}
return 0;
}
If you dont set the locale like [outputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]]; it will lead do a ugly mix of formats with user's sefault locale, if it not happend to be 'en_US'
ie in German: 31/12/2012 02:00:00 nachm. This is a format not used in german.
To support user's language and locale you should instead use the different styles.
The q&a rmaddy provided indicates, that there are rare cases, where the parse format gets rewritten. To avoid this, set the input formatted's format to a certain locale, like [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
You need to create two date formatter, one for parsing, one for formatting; Your parsing format is "yyyy-MM-ddThh:mm:ss" and your output format is "dd/MM/yyyy hh:mm:ss a". So, something like this :
NSString * targetString = #"1900-01-01'T'11:00:00";
NSDateFormatter *parseFormat = [[NSDateFormatter alloc] init];
[parseFormat setDateFormat:#"yyyy-MM-ddThh:mm:ss"];
NSDate * date = [parseFormat dateFromString:targetString];
NSDateFormatter * outputFormat = [[NSDateFormatter alloc] init];
[outputFormat setDateFormat:#"dd/MM/yyyy hh:mm:ss a";
NSString * outputString = [parseFormat stringFromDate:date];
example for getting current date :
-(void) getDate
{
NSString *theDate;
NSString *theTime;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
theDate = [dateFormat stringFromDate:now];
theTime = [timeFormat stringFromDate:now];
//2012-09-21 02:00:00
self.deviceCurrentTime=[NSString stringWithFormat:#"%# %#",theDate,theTime];
}
I am new in iphone, i want to change string into accurate date-formate which i couldn't done this for last 2 hrs. I have string like(17:30 18 Oct) and want to convert this in accurate formate and also compare this date to tomorrow if this in tomorrow then display just 18 Oct and if it on today just display time 17:30, any help please.
NSString to NSDate conversion
NSString *dateString = #"6:30 18 Oct";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm dd/MMM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
Try this
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"hh:mma dd/MMM"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(#"date: %#", dateString);
[dateFormat release];
I have struggling with to convert nsstring to nsdate format. I have a date in nsstring format like (2011-08-25 18:30:00 +0000), but i want to change it in nsdate format (08/26/2011). How to change it? Can any one help me please? Thanks in advance... I have spend one day to solve this but i cant find out the solution.. Please help me friends....
This is my code,
NSString *dateString = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
NSLog(#"Converted date is %#",dateFromString);
Output is,
Converted date is (null)
Yuva.M
NSString *dateWithInitialFormat = #"2011-08-25 18:30:00 +0000";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:dateWithInitialFormat];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *dateWithNewFormat = [dateFormatter stringFromDate:date];
NSLog(#"dateWithNewFormat: %#", dateWithNewFormat);
From NSLog: dateWithNewFormat: 08/25/2011
See Date Format Patterns
You need to use an NSDateFormatter and set the date format to match how you're storing it, like the below.
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *myDate = [df dateFromString: myDateString];
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MMMM dd, yyyy"];
NSDate* d = [df dateFromString:#"January 06, 1992"];
NSLog(#"%#", d);
NSDate *dispalyDate = d;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd MMM"];
NSString *str = [dateFormat stringFromDate:dispalyDate];
NSLog(#"%#", str);
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];
I am using the below code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
NSDate *today=[NSDate date];
NSString *string = [dateFormatter stringFromDate:today];
NSLog(#"%#",string);
[dateFormatter release];
This will not give me this format 03/20/11 11:24 AM(MONTH/DATE/YEAR HOUR:MIN), if i convert system date and time so the converted string should have such type of information 03/20/11 11:24 AM(MONTH/DATE/YEAR HOUR:MIN).? What should i do?
you should set the date format as,
[dateFormat setDateFormat:#"dd/MM/YYYY hh:mm a"];
Try doing this and u will get the format
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"MM/dd/yy HH:mm"];
NSDate *today=[NSDate date];
NSString *string = [formatter stringFromDate:today];
NSLog(#"%#",string);
[formatter release];
Good Luck!