Format NSDate from string [duplicate] - ios

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.

Related

Converting timestamp string with T and Z characters to NSDate [duplicate]

This question already has answers here:
Is there a simple way of converting an ISO8601 timestamp to a formatted NSDate?
(8 answers)
Closed 8 years ago.
I don't know how to convert this string to NSDate.
Date is in this format "2012-10-21T07:00:00Z"
I guess the problem is that I have no clue what those T and Z mean in the string :)
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-ddTHH:mm:ssZ"];
NSDate *dateStart = [formatter dateFromString:#"2012-10-21T07:00:00Z"];
// format that I want
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
NSLog(#"date: %#", [formatter stringFromDate:dateStart]);
all dates are in that format: 2014-09-12T03:46:25Z
That is an ISO date. The Z stands for Zulu but means UTC time.
Try:
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
To feed your initial nsdate.

Getting one date less [duplicate]

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);

Wrong Conversion [duplicate]

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).

ios convert NSString into date? [duplicate]

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.

NSDateFormatter giving incorrect output with am/ pm [duplicate]

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.

Resources