I have been sitting with this for quite sometime now. I'm tot able to figure out, what I am doing wrong.
NSString * dateString = #"2011-11-21 13:00";
NSDateFormatter *serverDateFormatter = [[NSDateFormatter alloc] init];
[serverDateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *serverDate = [serverDateFormatter dateFromString:dateString];
NSLog(#"%#", serverDate);
Output :
2011-11-21 12:00:00 +0000
Why does 13:00 get converted to 12:00? What am I doing wrong?
You need to set the time zone, example
NSString * dateString = #"2011-11-21 23:20";
NSDateFormatter *serverDateFormatter = [[NSDateFormatter alloc] init];
[serverDateFormatter setDateFormat:#"yyyy-MM-dd HH:mm"];
[serverDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *serverDate = [serverDateFormatter dateFromString:dateString];
NSLog(#"%#", serverDate);
OUTPUT:
2011-11-23 14:22:10.924 xxx [23235:207] 2011-11-21 23:20:00 +0000
I think you might find the answer in this discussion. Apparently NSDateFormatter likes to use the user's settings, which may not be appropriate.
http://developer.apple.com/library/ios/#qa/qa1480/_index.html
Though I think this answer is for stopping 24-hour behavior, which is what you want to force.
I'd say you want to get the setLocale part right.
ETA: Here's some more info on this:
https://stackoverflow.com/q/7538489/290072
This will allow your code to detect whether the device will try to do 12hr or 24hr times.
What if you output via [serverDateFormatter stringFromDate:serverDate]; ?
Related
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;
}
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.
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 ;)
I'm new to iPhone development. I want to set default date to NSDate Object as string. I don't see any easy way or method...
I think there might be a method in NSCalender? If there's such a method, please tell me.
Thanks in advance.
I'm not totally clear on what you are asking, but to create an instance of an NSDate object with the current date, one calls:
NSDate * myDate = [NSDate date];
If you are saying that you have a c-string or NSString that needs to be parsed to initialize an NSDate object, that's another question.
I have some code posted here:
How get a datetime column in SQLite with Objective C
that shows how to create NSDates from NSStrings using NSDateFormatter.
If you want to create an NSDate from a string, you need to use an NSDateFormatter to do it. It's important to note that the formatter will use the current locale's time zone when constructing the date, unless you put a time-zone in as part of the format. For more information about constructing time zones, see NSTimeZone.
For example, to create a date using the ubiquitous format '2011-01-16 00:00' in UTC, you would do:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
// Only certain abbreviations are okay, like UTC. See docs for more info
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate* midnight_26_jan_2011_utc = [formatter dateFromString:#"2011-01-26 00:00"];
// this will display in your system locale
// (for me, it shows 2011-01-25 19:00 +0500 because I'm America/New_York time)
NSLog(#"date: %#", midnight_26_jan_2011_utc);
[formatter release];
Edit: Added time to format string.
You will need to look at the NSDate and NSDateFormatter classes. Here's a simple example of how to use them:
NSString* defaultDateString = [NSString stringWithFormat:#"2011-01-22 15:30:00"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate* defaultDate = [dateFormatter dateFromString:defaultDateString];
[dateFormatter release];
and if you wanted to get the string from a date you can just use:
NSString* defaultDateString = [dateFormatter stringFromDate:defaultDate];
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; //here,you can set the date format as you need
NSDate *now = [[[NSDate alloc] init]autorelease];
NSString *theDate = [DateFormatter stringFromDate:now];
Now, you can use the string the date. :)
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
[dateFormatter setDateFormat:#"yyyy-mm-dd"];
NSDate *yourDate = [dateFormatter dateFromString:#"2011-01-26"];
I'm having a problem. I get incoming time strings in 12-hour format, and I'm turning them into NSDate objects. When the iPhone is in 12 hour format, no problem. But when it's in 24 Hour format, things go wrong. Here's some sample code to demonstrate:
NSString *theTime = #"3:19 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"h:mm a"]; // "3:19 PM"
NSDate *date = [formatter dateFromString:theTime];
NSString *theString = [formatter stringFromDate:date];
In 24 hour mode, date is 1970-01-01 03:19:00, and theString is "3:19" - WRONG
In 12 hour mode, date is 1970-01-01 15:19:00, and theString is "3:19 PM" - RIGHT
So... question 1: why is the device's 24 hour setting overriding my date formatter setting?
and more importantly, question 2: How do I get a proper conversion from 12 hour time to 24 hour time?
I already have code to detect if the phone is in 24 hour mode, but other than digging around in the string and swapping the 3 with a 15, there doesn't seem to be a clean way to do this.
Not sure if you still need it, but I've had a similar problem which got solved by setting the locale for the date formatter. That is, if you want to force it to 12-hour mode, regardless of the user's 24/12 hour mode setting, you should set the locale to en_US_POSIX.
The reason for this behaviour is Locale, set the correct Locale
NSString *strAgendaDate = #"01/17/2012 12:00 AM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
[dateFormatter setDateFormat:AgendaDateFormatForMeeting];
NSDate *meetingDate = [dateFormatter dateFromString:aStrDate];
[dateFormatter setDateFormat:AgendaDateRepresentation];
strAgendaDate = [dateFormatter stringFromDate:meetingDate];
It works for both 24-hour and 12 hour format
I believe the #"h:mm a" should be #"HH:mm a".
If you use the pre-build dateformatter in cocoa, everything will be taken care of for you.
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
[timeFormatter setDateStyle:NSDateFormatterNoStyle];
NSDateFormatterShortStyle and NSDateFormatterNoStyle comes in different varieties.
Using those will make sure you respect the settings the user has selected for dates and times.
The 12-14 hour clock conversion is taken care of by the SDK, if you have a model or some value object for storing your dates try to keep them as NSDate. This way you can format them only when you need to display them. Saving dates as strings could open a world of trouble when you maybe parse them from xml where the GMT is specified separately or try to add and subtract NSTimeIntervals.
I changed from #"hh:mm:ss" to #"HH:mm:ss" and time style was changed from "1:03 PM" to "13:03".
Hope this will help you.
Okay, I left a comment, but it squished all the code together, so I'll have to "answer" my question with a comment:
Thanks. I gave it a whirl with this code:
NSString *theTime = #"3:19 PM";
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc] init] autorelease];
[timeFormatter setTimeStyle:NSDateFormatterShortStyle];
[timeFormatter setDateStyle:NSDateFormatterNoStyle];
NSDate *date = [timeFormatter dateFromString:theTime];
NSString *theString = [timeFormatter stringFromDate:date];
And date comes up nil. I ran into this earlier when I tried this route, and it's not working. Very frustrating.