NSDateFormatter formats wrong for timezone - ios

I have a problem with Date Format
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"YYYY-MM-dd hh:mm:ssZZ"];
formatedDate =[format stringFromDate:datePicker.date];
Everything works fine, except one thing
when I set the YEAR less than or equal to 1924, DateFormatter returns
1924-04-21 03:00:07+050748
Whereas should be
1924-04-21 03:00:07+0500
What's wrong?

You can see the Apple Data Formatting Guide https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
In the "Fixed Formats",
There are two things to note about this example:
It uses yyyy to specify the year component. A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar. In most cases, yyyy and YYYY yield the same number, however they may be different. Typically you should use the calendar year.
The representation of the time may be 13:00. In iOS, however, if the user has switched 24-Hour Time to Off, the time may be 1:00 pm.
So you should change "YYYY" to "yyyy".

User this method .......
-(NSString*) currentDateForServer
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:UTC];
[dateFormat setTimeZone:timeZone];
[dateFormat setDateFormat:DATE_FORMATE];
NSDate *currentDate = [NSDate date];
NSString *finalString = [dateFormat stringFromDate:currentDate];
return finalString;
}
Define UTC and DATE_FORMATE as you need

You can set your Timezone, like with GMT,
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"yyyy-MM-dd hh:mm:ssZZ"];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[format setTimeZone:gmt];
formatedDate =[format stringFromDate:datePicker.date];
This may change your time in output string formatedDate, but you can set NSTimeZone as per your requirement.

Related

Converting calendar date to the correct date

I am working on an application that creates alerts with calendar. I can correctly set alarms on correct dates. For example, I set an alarm for 4th of May 2017 1 PM.
When, I try to get the calendar event it returns me some other date in UTC.
As you can see, it returns me 10 AM on same day with UTC. I am wondering how can I get the exact date when I try to get it from calendar.
You just need to convert UTC to your local timezone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate *date1 = [dateFormatter dateFromString:#"2017-05-04 10:00:00"];
// change to a readable time format and change to local time zone
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *strCurrentLocalTimezoneDate = [dateFormatter stringFromDate:date1];
Date always takes current time zone until we changed other.If we print the Date it might be showing different but actually it takes current.
// except this code you may have to set timeZone as well.
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM-dd-yyyy"];
NSDate *now = [[NSDate alloc] init];
NSString *dateString = [format stringFromDate:now];
NSLog(#"%#",dateString);

Generate NSDate of this type: 2015-04-27T10:45:52Z

I am trying to set the date to PayPal pre-approval key in the following way:
#"2015-04-27T10:45:52Z", #"startingDate",
This date works, however I don't know how to reproduce it in code terms. I tried doing:
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
[dateformate setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"]; // Date formater
NSString *date = [dateformate stringFromDate:[NSDate date]];
but this doesn't work. What is the Z at the end of the date?
First, your date format is not correct. Second, for consistent results, you should always hard-code the en_US_POSIX locale (the date formatter defaults to the user's locale):
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
Alternatively, I've had positive experience with iso-8601-date-formatter. ISO 8601 is a surprisingly complex standard with lots of edge cases, and this library seems to be able to cope with most of them.
The Z stands for Zulu (i.e. UTC/GMT). If you want to generate a date string in that format (GMT with Z qualifier), please refer to Apple's Technical Q&A #1480, which reminds us to specify both locale and timeZone:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
Or in macOS 10.12 and iOS 10, you can do:
NSISO8601DateFormatter *formatter = [[NSISO8601DateFormatter alloc] init];
And then you can do:
NSString *dateString = [formatter stringFromDate:[NSDate date]];

Converting date in string to NSDate type giving wrong Month and Year in iOS? [duplicate]

I am using the NSDateFormatter's dateFromString, yet when it is given the string '2013-03-06' it spits out an NSDate of '2012-03-06 00:00:00 CST'. The time is of no importance. I have the date format set to be exactly the same between the input and output.
I have an object which gets a date set using the code:
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
NSDate* currentDate = [[NSDate alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
[self setMyValue:[dateFormat stringFromDate:currentDate]];
And then this value is later used to to set the date on a UIDatePicker:
NSDate *date = [[[NSDate alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
date = [dateFormat dateFromString:myFilter.myValue];
[datePicker setDate:date animated:YES];
Any pointers on where I am erring would be much appreciated.
You should use yyyy and not YYYY.
From Data Formatting Guide:
A common mistake is to use YYYY. yyyy specifies the calendar year
whereas YYYY specifies the year (of “Week of Year”), used in the ISO
year-week calendar
Change YYYY in the format to yyyy.
Use yyyy instead of YYYY, to get the zero error while changing 'YEARS'.

iOS 'dateFromString' Returning One Year Off

I am using the NSDateFormatter's dateFromString, yet when it is given the string '2013-03-06' it spits out an NSDate of '2012-03-06 00:00:00 CST'. The time is of no importance. I have the date format set to be exactly the same between the input and output.
I have an object which gets a date set using the code:
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
NSDate* currentDate = [[NSDate alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
[self setMyValue:[dateFormat stringFromDate:currentDate]];
And then this value is later used to to set the date on a UIDatePicker:
NSDate *date = [[[NSDate alloc] init] autorelease];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd"];
date = [dateFormat dateFromString:myFilter.myValue];
[datePicker setDate:date animated:YES];
Any pointers on where I am erring would be much appreciated.
You should use yyyy and not YYYY.
From Data Formatting Guide:
A common mistake is to use YYYY. yyyy specifies the calendar year
whereas YYYY specifies the year (of “Week of Year”), used in the ISO
year-week calendar
Change YYYY in the format to yyyy.
Use yyyy instead of YYYY, to get the zero error while changing 'YEARS'.

Formatting month and year using NSDateFormatter returns invalid date

I have set the locale and timezone but when I format the date, I always get invalid date. The month is always December and year is one less that the specified year. In my case I dont need the day component.
I checked other similar post but it didn't solved the problem.
Any help is appreciated. Thank you.
Here is the code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"] autorelease];
dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setDateFormat:#"MMM YYYY"];
NSDate *formattedDate = [dateFormatter dateFromString:#"Sep 2013"];
NSLog(#"Date: %#", [dateFormatter stringFromDate:formattedDate]);
[dateFormatter release]; dateFormatter = nil;
OUTPUT: Date: Dec 2012
"YYYY" format is used for "Week of Year" based calendars. Use "yyyy" format specifier instead:
[dateFormatter setDateFormat:#"MMM yyyy"];
I just wanted to add something to the great answer by Vladimir. If you do this before setting your locale, it seems that the date formatting goes crazy. What I had to do was to set the locale before setting the new format, and then use the setDateFormat to change the format based on the locale used.So something like this would do:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];//I wanted to set the locale to wherever user is using my app in
[formatter setDateFormat:#"MMM YYYY"];
NSDate *now = [NSDate Date];
NSString *fancyLookingDate = [formatter stringFromDate:now];

Resources