Getting wrong time using NSDateFormatter - iphone

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.

Related

Convert NSString to NSDate not correct

in my iPhone application I need to convert NSString to NSDate with formaT. Here is my code:
+(NSDate*)dateFromJsonString:(NSString*)string{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'"];
NSLog(#"%#", [dateFormat dateFromString:string]);
return [dateFormat dateFromString:string];
}
If parameter "string" is 2013-05-30T15:53:02 after converting - [dateFormat dateFromString:string] it becomes 2013-05-30 12:53:02 +0000 , so there is a difference in 3 hours. How can I fix it?
You need to set the correct time zone. Here is a sample, correct it to the time zone of your server:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
[dateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'"];
return [dateFormatter dateFromString:dict[#"completed_at"]];
EDIT: You can check the available time zones with a class method on NSTimeZone:
+ (NSArray *)knownTimeZoneNames
Just output it to the console, and choose the time zone that corresponds to the server one.
EDIT 2: Probably this is more useful:
+ (NSDictionary *)abbreviationDictionary
Choose your time zone and than just change the #"UTC" in the code above with the desired abbreviation.
Try this dateFormat. Since timezone is not given in dateString, dateFormatter assumes it to be local timezone. If you know the timezone set it to formatter.
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
Check this answer for more details.
This is TimeZone issue so use this code to fix this problem :
+(NSDate*)dateFromJsonString:(NSString*)string
{
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'"];
[dateFormat setLocale:locale];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"%#", [dateFormat dateFromString:string]);
return [dateFormat dateFromString:string];
}
Hope it helps you.
You can use the following code to set timezone.
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
But before you try that, i should tell you that if you are logging the date, it will show it in UTC format and not in the timezone of your computer. So dont check your date by printing it. So try converting the date to string and printing it to check if the date is correct or not.
Try logging NSLog(#"%#",[dateFormatter stringFromDate:date]); first
You just need to set current time zone for date formatter. Because of it, you got some time difference. Use below code..
+(NSDate*)dateFromJsonString:(NSString*)string
{
NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[inputFormatter setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss"];
NSDate *xExpDate = [inputFormatter dateFromString:string];
return xExpDate;
}

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.

How to convert time formatter in iPhone?

Currently i am working in iPhone application, Here i have using NSString to store (2012-07-11 11:49:55 +0000) and using NSDateFormatter to convert the date and time but the output comes 05:19:55, but i want 11:49:55, please any one help me
Here i tried the source code for your reference
NSString *dateString = [NSString stringwithformat:%#,(2012-07-11 11:49:55 +0000)];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"hh:mm:ss";
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSLog(#"The Current Time is %#",[dateFormatter stringFromDate:dateString]);
[dateFormatter release];
Your system time zone is not +0000. So the time is formatted according to it. The solution is to set same time zone to GMT (+0000).
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
The reason is you are changing the TimeZone, please comment out the following line and it should give back the time you provided in the dateString:
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

NSDateFormatter not working properly?

My code ,
-(NSString *)trimDateStringInRequiredFormat:(NSString *)dateInString{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateInString];
[dateFormatter setDateFormat:#"hh:mm aaa"];
NSString *dateDisplay = [dateFormatter stringFromDate:date];
return dateDisplay;
}
The above worked properly when my date is 2012-06-06 12:00:00 and I got appropriate output like 12:00 AM. But when I send my date like 2012-06-06 15:00:00 it doesn't return me appropriate output, instead, it returns me null output. When I'm trying to trace this function the 2012-06-06 15:00:00 date on NSDate *date = [dateFormatter dateFromString:dateInString]; this line not properly converted. Why did this happen? where am I wrong in this code???
try doing:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
With hh you have the 12h format, with HH you use 24h format
This is because for 24 Hr format you need to use HH instead of hh

Problem in setting nsdate value from a string

NSDate *My_StartDate,*My_EndDate ;
NSDateFormatter * df= [[NSDateFormatter alloc]init];
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
My_StartDate = [df dateFromString:#"01/05/2010 10:15:33"];
My_EndDate = [df dateFromString:#"01/05/2010 10:45:33"];
NSLog(#"%#",My_StartDate);
NSLog(#"%#",My_EndDate);
In the log i get something like this for the my_startdate as 2010-05-01 04:45:33 +0000 and end date as 2010-05-01 05:15:33 +0000 instead i should have got value as for start date as 2010-05-01 10:15:33 +0000 and end date as 2010-05-01 10:45:33 +0000
Try with below function:
-(NSString *)getDateStringFromDate :(NSDate *)dateValue{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setTimeStyle:NSDateFormatterShortStyle];
[timeFormat setDateFormat:#"HH:mm:ss"];
//[timeFormat setDateFormat:#"HH:mm"];
//[timeFormat setDateFormat:#"HH:mm a"];
////
NSString *theDate = [dateFormat stringFromDate:dateValue];
NSString *theTime = [timeFormat stringFromDate:dateValue];
NSLog(#"\n"
"theDate: |%#| \n"
"theTime: |%#| \n"
, theDate, theTime);
return theDate;
}
Change Format of data as per your need.
Let me know in case of any difficulty.
Cheers.
This shows date which follow American standard time string but by this reason you don't get any problem in making your logic.Also
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
this format using 12 hour format (means 2:03 pm and 2:03 am) and date object never use am and pm for showing date object value but when you convert it correctly then it gives you right date and time.
If you feel you get any problem then use different locale for that.
It is displaying asper the GMT+4.30 time.It displays like that only.When you are converting that date to string using the DateFormatter it gives the same date(Whichever you want like start date as 01/05/2010 10:15:33 and end date as 01/05/2010 10:45:33).
NSDateFormatter * dateformatter= [[NSDateFormatter alloc]init];
[dateformatter setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
NSString *dat = [dateformatter stringfromDate:My_StartDate];
then you will get the output as 01/05/2010 10:15:33
You might want to set the time zone of the date formatter to GMT here. Do it using
[df setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
before you do dateFromString: calls. This will give you what you want.
Just need to update here in your code:
I might be like that your time would be in 24 hours format, so at that time you need to use this ....other than that you need to set the timezone.
Follow this link for All zone : http://unicode.org/reports/tr35/tr35-6.html#Date%5FFormat%5FPatterns
[df setDateFormat:#"dd/MM/yyyy hh:mm:ss"];
to
[df setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
You are Done ;)