I have this code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm Z"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setLocale:[NSLocale currentLocale]];
NSString *sDate = [dateFormat stringFromDate:_datePicker];
_datePicker comes from this and datePicker is a UIDatePicker
_datePicker = datePicker.date;
This was working ok in iOS 7 fore sure, and now I'm testing in 9.1
So if the phone settings is set to show the time in the 24h format its ok, but if I change the settings to none 24h format I have a estrange behavior:
if is set to 24h the output is:
But if i change it to non 24h is:
As you can see is like is combining the 2 formats it adds for example the 13 of the 1 pm to the hour 131:00 or at 6 pm will be 186:00.
Is there a work around to always get the time in the 24h format?
Thanks in advance.
Hi I have sean this approach in another page and I have change the code to this:
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:#"yyyy-MM-dd HH:mm Z"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *sDate = [rfc3339DateFormatter stringFromDate: _datePicker];
And now is working properly but I'm not sure that this is the correct way.
I hope this helps someone in the future.
UPDATE: This is where I got the code.
Related
In Following Code I am trying to convert NSString to NSDate and once again converting to another Date format and sending it to server.
Following code worked perfectly fine when my Device Time Format is 12 hr. with dateformat to [dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm a"]; it gives me correct Date and Time.
But As soon as I changed device time Format to 24 hr. and change the datefomatter to [dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"]; it gives wrong Date and Time (Specifically time.) I had shown log values for code.
I had checked lot of stackoverflow questions regarding NSDateFormatter and Conversion of NSString to NSDate. But I am not able to find out where I'm making mistake and why it gives me wrong time when my Device time format is 24hr. Please help me to resolve this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HH:mm a"];
NSString *fromTime = [NSString stringWithFormat:#"%#",#"04-Jul-2017 01:46 PM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:fromTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *setdtFrom=[dateFormatter stringFromDate:dateFromString];
NSLog(#"From DateTime %#",setdtFrom);
Following log showing Date and Time When My Device Time format is 12hr. And it is Correct one.
From DateTime 2017-07-04T13:46:00
Time When My Device Time format is 24hr. And it is wrong (specifically time).
From DateTime 2017-07-04T12:46:00
Please try setting locale & timezone
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MMM-yyyy hh:mm a"];
[dateFormatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier: #"en_US_POSIX"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *fromTime = [NSString stringWithFormat:#"%#",#"04-Jul-2017 01:46 PM"];
NSDate *dateFromString = [dateFormatter dateFromString:fromTime];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSString *setdtFrom=[dateFormatter stringFromDate:dateFromString];
NSLog(#"From DateTime %#",setdtFrom);
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]];
I am trying to get the user's local Date & Time using the below code
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"dd MMM yyyy"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat: #"HH:mm"];
[timeFormatter setTimeZone:[NSTimeZone localTimeZone]];
self.appearedOnDate = [dateFormatter stringFromDate:[NSDate date]];
self.appearedOnTime = [timeFormatter stringFromDate:[NSDate date]];
It returns correct values, but in the language set on users device.
For example, in a case where user has Simplified Chinese set on his device, I am getting the output as
24 3月 2015 --- 22:13
How can I get the local time in English language ?
I guess this happens only in iOS8 and not on iOS6, but I am not sure.
You might add the following code before print your output.
[NSLocale availableLocaleIdentifiers];
NSLocale *locale = [[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[timeFormatter setLocale:locale];
This is really odd, the code below takes self.danceTimeIn (its text state) and converts it to an actual time. The problem is that its coming up 1 hour LESS than what's entered. Meaning that if I enter 14:03 I'll get 13:03 in the database! The same thing is happening with the date version of this code
++++++++++++++++++++++
TIME
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:#"HH:mm"];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
DATE
NSString *danceDateValue = self.danceDate.text;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yy"];
NSDate *date = [dateFormat dateFromString: danceDateValue];
++++++++++++++++++++++
Anyone ???
Try using this. NSDateFormater change date according to locale of your device settings, if you set locale properlyl, you will get proper date. Try if this works :)
NSString *danceTimeIn = self.danceTimeIn.text;
NSDateFormatter *timeFormatIn = [[NSDateFormatter alloc] init];
[timeFormatIn setDateFormat:#"HH:mm"];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[timeFormatIn setLocale:usLocale];
NSDate *timeIn = [timeFormatIn dateFromString: danceTimeIn];
Time zone may be causing this problem.Try
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
May be this will help.
It depends on the timezone:-
first Check your local time zone
NSTimeZone *tz=[NSTimeZone timeZoneWithName:#"GMT"];
[dateformat setTimeZone:tz];
and then set your date accordingly.
I have an NSDate object from which I make two NSStrings: The date and the time. Currently I format the date as 20111031 and time as 23:15.
What I would like to do is to format it to the device (iPhone, iPad, iPod Touch) current region settings (not the language!). So for instance:
A device set to region US would show (from the top of my head) 10.31.11 and time 11:15 pm
A device set to region the Netherlands would show: 31-10-2011 and time 23.15
A device set to region Swedish would show: 2001-10-31 and time 23:15
How can I do this?
The following should be enough, because an NSDateFormatter has the phone's default locale by default:
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
FYI here's what happens with US, Netherlands, and Sweden:
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
// displays 10/30/11 7:09 PM
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"nl_NL"]];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
// displays 30-10-11 19:09
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"sv_SE"]];
NSLog(#"%#",[dateFormatter stringFromDate:date]);
// displays 2011-10-30 19:09
Many great code snippets here. One even better in my mind for international date formats (when all I want is the date, not the time) is this as the phone knows the locale language in settings:
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:NSLocaleIdentifier]];
[dateFormatter setTimeStyle:NO];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
NSLog(#"%#",[dateFormatter stringFromDate:date]);