consider my code bellow -
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"EEEE"];
NSString *weekDay = [dateFormat stringFromDate:today];
NSLog(#"%#", weekDay);
return weekDay;
when i am running my app in xcode then it does work nicely and showing it in english . But when i run it on iphone it is showing in danish in few area. Even though after setting iphone default language in english it shows then in danish also. But when i change region in US then it shows in english.
Can you suggest me how i can fix this problem. Just i want to show it in english when language is set in english.
You need to tell NSDateFormatter the local that is being used.
To get the systems current local use:
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
dateFormat.locale = [NSLocale currentLocale]
[dateFormat setDateFormat:#"EEEE"];
NSString *weekDay = [dateFormat stringFromDate:today];
NSLog(#"%#", weekDay);
return weekDay;
Related
I am creating an app where the current date needs to be displayed in a label and automatically updated each day. I have looked at a few tutorials and they have all shown how to get the date in YYYY:DD:MM hh:mm, I am wondering how to change this format to DD/MMM/YYYY.
Any sample code or links to tutorials would be greatly appreciated.
Something like this:
NSDate *today = [NSDate date];
NSDateFormatter *formatter = [[NSDateformatter alloc] init];
[formatter setDateFormat:#"dd/MMM/yyyy"];
[myLabel setText:[formatter stringFromDate:today]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"dd/MM/yyyy"];
NSString *dateString = [dateFormatter stringFromDate: dayDate];
//display dateString in label
[dateFormatter release];
You need to use the NSDateFormatter. For specfic formats look at
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"dd/MMM/yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
My App is a JSON code which involves a date in string type. (Such as "2011-10-01").
I would like to know how I could conver it into NSDate?
It needs to be displayed in a different time format such as "1 October, 2011".
ex. this code doesn't work:
NSString *date1 = #"2010-11-12";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MMMM-dd"];
NSDate *date2 = [dateFormat dateFromString:date1];
NSString *strdate = [dateFormat stringFromDate:date2];
As Tug writes in his blog post on the subject:
Objective-C and iOS SDK provide a class to help formatting date
(marshaling and unmarshaling), this class is NSDateFormatter. No
surprise, the NSDateFormatter uses the Unicode Date Format
Patterns.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:publicationDate ];
[dateFormatter release];
where publicationDate in this case is an NSString.
Use NSDateFormatter. The appropriate method is dateFromString:. Take a look at the documentation :)
You can try this
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"dd MM yyyy"];
NSString *todayString = [df stringFromDate:[NSDate date]];
NSDate *someDate=[NSDate date];
NSString *targetDateString = [df stringFromDate:someDate];
NSLog(#"date:%#",targetDateString);
I have used this code to show current day name. When runned into XCode, it is displayed in english, but when runned onto the iPhone, it shows the result in danish.
So how I can fix it ?
-(NSString *)selectWeekDate:(NSDate*)date
{
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateFormat:#"EEEE"];
NSString *weekDay = [dateFormat stringFromDate:today];
//[dateFormat release];
NSLog(#"%#", weekDay);
return weekDay;
}
You can set the NSDateFormatter locale
- (void)setLocale:(NSLocale *)locale
You can make an english locale like this
NSLocale *gbLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"] autorelease]; // for brits
NSLocale *usLocale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]; // for americans
// etc...
Always i'm struggling with NSDate in iphone apps. I have developed one iphone app for US based client. The app should show the todays date. I have used the following code in my app. The date is showing correctly for me(Am in India). But, the date is also showing the india date for my client in (US). I want to show the date from the iphone/ipad device.
NSDate *today1 = [[NSDate alloc] init];
NSString *dates = [NSString stringWithFormat:#"%#",[today1 description]];
dates = [dates substringToIndex:10];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterBehaviorDefault];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dates];
How to do this? Any suggestions/sample code to solve this? I has spend 6 hours to solve my problem but, i didn't find the answer? Please help me. Thanks to spend your valuable time with me.
Use Locale
See code blow Taken from here
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_GB"];
NSString *dateFormat;
NSString *dateComponents = #"yMMMMd";
dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:usLocale];
Updated answer below
I have tested this should work out of the box any where is the world.
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
[dateFormat setLocale:[NSLocale currentLocale]];
NSString *dateString = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"Date: %#:", dateString);
Try using
[dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease]];
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];