This question already has answers here:
Get NSDate from NSDate adjusted with timezone
(2 answers)
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 9 years ago.
Below is what I am using
NSString *myDate = #"01-11-2014 10:22 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:mm a"];
NSDate *newDate = [dateFormatter dateFromString:myDate];
NSLog(#"new date===%#=====%#", newDate, myDate);
Below is what I am getting
new date===2014-11-01 19:22:00 +0000=====01-11-2014 10:22 PM
^^
Output I was expecting is
new date===2014-11-01 22:22:00 +0000=====01-11-2014 10:22 PM
^^
Any idea what is going wrong?
When I have AM I have below output
new date===2014-11-01 07:22:00 +0000=====01-11-2014 10:22 AM
^^
Edit 1
Actually what I am doing is asked date and time in UITextField (sadly but true as client wanted it in same way)... and then concatenating this string and converting it to NSDate.
So what I have is
NSString myDate = [NSString stringWithFormat#"%# %#", appDate, appTime];
Add this line of code:
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
You probably had the wrong timezone. You have to set the timezone to your timezone.
Related
This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 8 years ago.
I have some trouble to convert this string :
#"15/08/2014 08:30"
Into an NSDate, here my code :
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setDateFormat:#"dd/MM/yyyy HH:mm"];
When I print out [formatter dateFromString:fullDatesString]; (fullDatesString is my string) the result is :
2014-08-15 06:30:00 +0000
Why my time is less than 2 hours between my string and my NSDate object ?
When you print the description of the NSDate, it prints in in GMT (+0000) regardless of what timezone you or the NSDate is in.
If you convert that time to your timezone (GMT + 2 I assume), then the time is correct.
This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 8 years ago.
I don't why i'm getting one date less, when I'm converting a string from a date, i'm getting one date less, e.g. when i'm converting 18/06/2014, i'm getting 2014-06-17, Any idea why this problem, my codes are:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
This is what I'm getting wholly from the log: 2014-06-17 20:00:00 +0000
You will have to take the timezone into account. Your current timezone seems to be ahead of GMT. If you print the entire date with say a time stamp, then you will get the difference. So i suggest you add the timezone to the NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
NSLog(#"Date : %#", date);
This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 9 years ago.
NSDateFormatter converting into wrong date don't know why
I am converting following string 19-01-2014 01:06:54 PM into date using following code
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"DD-MM-YYYY hh:mm:ss a"];
NSDate *date = [dateFormat dateFromString:startTime];
And i am getting following output which is incorrect.Please suggest some thing
Printing description of date:
2014-01-04 07:36:54 +0000
The "DD-MM-YYYY" part in your format string is not correct, is should be "dd-MM-yyyy".
(See http://www.unicode.org/reports/tr35/tr35-25.html#Date_Format_Patterns for a
full list of all date formats.)
Also you should set a "POSIX locale" to be independent of the user's locale/region
settings:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy hh:mm:ss a"];
[dateFormat setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *date = [dateFormat dateFromString:startTime];
Printing an NSDate will return the default description -- since NSDates don't take locale, timezone, etc. into consideration, it defaults to UTC +/- 0000 (notice the +0000).
This question already has answers here:
Converting a string to an NSDate
(6 answers)
Closed 9 years ago.
I have a string containing:
2013-10-29 18:50:18 +0000
How can i convert this into a date, but keeping the same date format?
I've tried:
NSString *str3 = [[NSString alloc] initWithFormat:#"%#", time];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd hh:mm:ss a"];
NSDate *myDate = [df dateFromString:str3];
But myDate returns (null)
Any ideas?
Try this format:
yyyy-MM-dd HH:mm:ss ZZZ
HH for hours, because you clearly have 24-hour clock
ZZZ for timezone, because you used a and that’s for AM/PM period
Full reference of Unicode Date Format Patterns.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Wrong time from NSDateFormatter
NSDate is 5 hours off
I am trying to convert NSString to NSDate with the code
result = #"2012-02-09";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:result];
[dateFormatter release];
NSLog(#"date: %#", dateFromString);
But the date conversion giving an incorrect result, in logs:
2012-02-07 13:08:29.553 Document[611:15503] date: 2012-02-08 19:00:00 +0000
Can someone please tell me whats wrong with my code?
To use NSLog to display a date you should use your formatter so that the NSDate will be formatted with your TimeZone:
NSLog(#"date: %#", [dateFormatter stringFromDate:dateFromString]);