iPhone NSDateFormatter US region style - iphone

I am working on writing iPhone app.
I want to get current date as following format.
Tur, 18 Apr 2013 09:20:47
I tried with following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
[dateFormatter setDateFormat:#"E, dd MMM yyyy HH:mm:ss"];
NSString *formattedDate = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
The problem is my iPhone region is not US, so I get following string.
木, 18 4月 2013 09:20:47
How can I resolve this?

Set up the locale property of date to get it in US format
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]
autorelease];
or
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];

I have't use NSDateFormatter from a long time but i am proving some links -
For better understanding you can refer -
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html and
https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
I hope, it might help you.

Related

How to display date format like "Today, 25 Mar 2014 15:18" in iOS?

I want to display date like Today, 25 Mar 2014 15:18, if the date is today's date.
Also expecting Tomorrow, 26 Mar 2014 15:18, if it is tomorrow's date and Wednesday, 27 Mar 2014 15:18, if it is day after tomorrow's date.
Anyone please help me.
Sample from developer.apple.com
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[dateFormatter setLocale:frLocale];
[dateFormatter setDoesRelativeDateFormatting:YES];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*1];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
NSDateFormatter *detailFormatter = [[NSDateFormatter alloc] init];
[detailFormatter setLocale:frLocale];
[detailFormatter setDateFormat:#"dd MMM yyyy HH:mm"];
NSString *detailString = [detailFormatter stringFromDate:date];
NSLog(#"detailString: %#", detailString);
NSString *finalString = [NSString stringWithFormat:#"%#, %#",dateString, detailString];
NSLog(finalString);
Apple doesn't directly support using relative date combined with absolute date. What you'd have to do is to create two formatters, one with relative enabled and one without. Then compare the output strings (e.g. "Today" and "Mar 25") and if they are not equal, combine them: "Today, Mar 25", but if they are equal, just use one.
And as an aside, actually on "après-après-demain", I think you may have found a bug (either in docs or code). Do you actually set "après-après-demain"? I tried Apple's sample code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLocale *frLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"fr_FR"];
[dateFormatter setLocale:frLocale];
[dateFormatter setDoesRelativeDateFormatting:YES];
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:60*60*24*3];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
Which Apple says produces output of dateString: après-après-demain no matter what your language is set to (as it switches the date formatter to French).
But when I ran the code on both iOS and OS X, I get the actual date: Mar 28 (and if I change to *2, I get après-demain.
So, if you're seeing the same, I'd suggest filing a bug report with Apple.

Getting wrong time using NSDateFormatter

When i am
[dateFormatter setDateFormat:#"HH:mm"]
using to fetch the current hour
i am getting correct time till 12:59 but after that it again starts from 1:00 i.e. 12:59 is displayed as 12:59 but 14:00 is displyed as 2:00.
Please let me if anyone is also having a similar problem and suggest a solution.
Thanks
Please check the code below.
NSDateFormatter *dateFormator = [[NSDateFormatter alloc] init];
dateFormator.dateStyle = NSDateFormatterMediumStyle;
dateFormator.dateFormat=#"HH:mm:ss";
NSCalendar * calender=[[NSCalendar alloc]initWithCalendarIdentifier:[NSGregorianCalendar autorelease]];
NSDateComponents * component=[calender components:NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:date];
I agree with
[dateFormatter setDateFormat:#"HH:mm"]
should give you the time in 24 Hours. But this doesn't work if the user set 24 hour to off. Despite using HH in a format string, the user setting overrides this. This might be the problem in your case!
Use this one
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"YYYY-MM-dd kk:mm:ss"];
[dateFormatter setLocale:[NSLocale currentLocale]];
NSString *dateString = [dateFormatter stringFromDate:date];
You can use
[dateFormatter setDateFormat:#"H:mm"]
for 24 hour time format
Set your dateformat to
[dateFormatter setDateformat:#"HH:MM a"];
Try this code
[dateFormatter setDateFormat:#"hh:mm:ss"];
HH : 24 hour format
hh : 12 hour format
So in your case
//after conversion to date
if ON
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
if OFF
[formatter setDateFormat:#"YYYY-MM-dd hh:mm:ss a"];
Hope it works for you.

How to get timezone in GMT Form in iOS6

In my iPhone app I want to get system timezone in GMT. Am Using the following code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"zzz";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
NSString *timeZoneString=[timeStamp substringFromIndex:3];
[dateFormatter release];
Am getting timezone in GMT form in iOS5 but not in iOS6. How to get timezone in GMT form in iOS6? Please Help!!!
You can check with,
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
in place of,
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
I have revised #ACB's code as there were a few typos , try [dateFormatter setTimezone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; instead of [dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];. This seems to work for me.
I would like to give credit for this answer to #ACB. He/she came up with this technique and I only confirmed it.

change date format in iPhone

I am trying this since last two hours and finaly left it out for you guys :P
I need to convert this String Mon, 14 May 2012, 12:00:55 +0200
into Date dd/mm/yyyy hh:ss format..
Any kind of help towards the goal will be really appreciated.
What I have tried
I have tried using NSDateFormatter but I am unable to figure out the exact format of the above date.
This [dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"]; is how I have tried and many other formats too
Eg:
NSString *finalDate = #"Tue, 29 May 2012, 14:24:56 +0200";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEddMM,yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:finalDate];
Here the date alwasys comes as nil
The most important part of date formatting is often forgotten, tell the NSDateFormatter the input language:
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"EEE, dd MMMM yyyy, HH:mm:ss Z"];
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"EN"];
NSDate *date = [dateFormatter dateFromString:#"Mon, 14 May 2012, 12:00:55 +0200"];
NSLog(#"date: %#", date);
I've checked the output: date: 2012-05-14 10:00:55 +0000
Be aware that the HH in the date formatter is for 24hr.
Now I would suggest not to use a fixed output scheme, but use one of the NSDateFormatterStyle:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormatter stringFromDate:date];
NSLog(#"date: %#", dateString);
Which in american english will output: 5/14/12 12:00 PM
This code is valid for ARC if you are not using ARC add autorelease to the NSLocale and release the NSDateFormatter after you are done with it.
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:#"dd/mm/yyyy hh:ss"];
NSString *formatterDate = [inputFormatter stringFromDate:inDate];
[inputFormatter release];
get inforamtion about all Date Formate
my Blog Link is http://parasjoshi3.blogspot.in/2012/01/date-formate-info-for-iphone-sdk.html
and also small function is bellow.....
-(NSString *) dateInFormat:(NSString*) stringFormat {
char buffer[80];
const char *format = [stringFormat UTF8String];
time_t rawtime;
struct tm * timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, 80, format, timeinfo);
return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding];
}
////////Like.......
NSString *mydate = [self dateInFormat:#"%Y%m%d-%H%M%S"];
hope,this help you....

dateFromString returning wrongDate

I am converting an NSString like #"12:25 AM May 27 2011" into NSDate.But as I convert it using the following code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a MMM dd YYYY"];
NSDate *date = [[dateFormatter dateFromString:#"12:25 AM May 27 2011"] copy];
[dateFormatter release];
I get the wrong Date as 2010-12-25 19:25:00 +0000
Can anyone please help me around here
It's not a wrong date, the time is displayed GMT hance (+0000) if you set the correct timezone very thing will be oke.