How to get the localized day of an NSDate as string? - iphone

I have an NSDate object and must create a label that indicates the day of the week in a short 3-char style like 'Mon', 'Tue', 'Wed', ... and localized to the short representation in any language.
The date formatter EEE has 3 chars but how does that translate to languages like Chinese which probably only have one character?

NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEE"];
NSString *strDate = [formatter stringFromDate:date];
[formatter release];
//Commented out the following line as weekday can be of one letter (requirement changed)
//strDate = [strDate substringToIndex:3]; //This way Monday = Mon, Tuesday = Tue

To use the correct locale you'll want to set the dateFormat on the instance of NSDateFormatter using the + dateFormatFromTemplate:options:locale: method.
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:#"EEE" options:0 locale:[NSLocale currentLocale]];
NSLog(#"Weekday using %#: %#", [[NSLocale currentLocale] localeIdentifier], [dateFormatter stringFromDate:date]);
// Test what Chinese will look like without changing device locale in settings
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"zh_CN"];
NSLog(#"Weekday using zh_CN: %#", [dateFormatter stringFromDate:date]);
This prints:
Weekday using en_US: Thu
Weekday using zh_CN: 周四

This will print what you need:
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"EEEE"];
NSString *day = [formatter stringFromDate:date];
[formatter release];
if ([day length] > 3) day = [day substringToIndex:3];
NSLog(#"%#", day);

Related

Get time from NSDate returns nil

I am trying to show time on graph and i have a full time stamp in this #"2012-08-28 18:50:24" format. when i try to get time from this date then it returns nil in NSDate.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
in this code only if I change [formatter setDateFormat:#"HH:mm:ss"]; line to [formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; then it starts working with full date and time. But i need only time
u have string . so you must have to convert it into date first as per your string format. after that you can convert date into any format. so follow this.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
[formatter setDateFormat:#"HH:mm:ss"];
NSString *str = [formatter stringfromdate:time];
NSLog(#"%#",str);
NSDateFormatter is used to format your NSDate to your requirements.
Here you are fetching the date from a string so you have to tell the NSDateFormatter to look for the string with that format and convert it into date
NSDate contains both date and time so if you want the time only to show use dateformatter to get the time component from NSDate
iOS manages time and date together in NSDate.Actually they are supposed to work together.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
// GET Proper NSDate
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
//To your requirement
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timestr=[formatter stringFromDate:time];
NSLog(#"%#",timestr);
Because, first you need to convert your string into NSDate in the right formate that is
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *aDate = [formatter dateFromString:#"2012-08-28 18:50:24"];
Now you can reformate the NSDateFormatter according to your need. Now you can get time from the
[formatter setDateFormat:#"HH:mm:ss"];
NSString *newDate = [dateFormatter stringFromDate:aDate];
If you need to produce Date from String then you'll need to set the date format accordingly.
This is important that you create a NSDate object first! Only then you can separate time from whole date.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
then
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:time];
You can also use other string concatenation functions to remove the Date Modules like this.
However, if you have an NSDate first, then your previous code would work.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:date];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"hh:mm:ss a"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];
NSLog(#"time, %#",theTime);
First you have converted your string date and time in the date formate..
YourTimeStr = #"2012-08-28 18:50:24";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-mm-dd HH:mm:ss";
NSDate *DateAndTime = [formatter dateFromString:LogOutTimeSTR];
then
NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
[dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSString *YourTimeStr =[dateFormatter1 stringFromDate:DateAndTime];

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];

How to change nsstring to nsdate format in iphone?

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);

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];

NSDateFormatter DateFromString from RSS Feed returning (NULL)

I have allocated a date formatter, and I am trying to format a string which contains a date:
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterShortStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *date = [[stories objectAtIndex:storyIndex] objectForKey:#"date"];
NSDate *formattedDate = [formatter dateFromString:date];
NSLog(#"%#", formattedDate);
As you can see I am 'NSLogging' the formattedDate to see if it had worked or not. Unfortunately it has not worked and therefore is returning (NULL).
Please could you tell me what I am doing wrong.
Thanks in advanced.
The string "Fri, 10 Dec 2010 23:48:36 +0000" is not a "short style".
To parse that reliably, set the locale and an explicit date format:
formatter = [[NSDateFormatter alloc] init];
NSLocale *locale = [[[NSLocale alloc]
initWithLocaleIdentifier:#"en_US_POSIX"] autorelease];
[formatter setLocale:locale];
[formatter setDateFormat:#"EEE, dd MMM yyyy HH:mm:ss Z"];
NSString *date = [[stories objectAtIndex:storyIndex] objectForKey:#"date"];
NSDate *formattedDate = [formatter dateFromString:date];
NSLog(#"%#", formattedDate);
Edit:
To then display the NSDate formattedDate in the format "Friday 10 December 2010", add this code after the above:
NSDateFormatter *displayFormatter = [[NSDateFormatter alloc] init];
[displayFormatter setLocale:locale];
[displayFormatter setDateFormat:#"EEEE dd MMMM yyyy"];
NSString *displayDate = [displayFormatter stringFromDate:formattedDate];
NSLog(#"displayDate = %#", displayDate);
[displayFormatter release];
For an explanation of the locale see Apple QA1480.
For an explanation of the date format see Unicode Date Format Patterns.