Hi I am converting a GMT time to local time in my project - I getting the correct value in all simulator and in iPhone/iPod - but when I run this in iPad I am getting null,
Here is my code -
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"HH:mm:ss";
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *startDate = [dateFormatter dateFromString:#"14:37:00"];
NSLog(#"startDate---%#",startDate);
//This is startDate---2000-01-01 14:37:00 +0000 (in simulator/iPod/iphone)
// startDate---(null) (in ipad)
Issue is with user's ipad's time format - if it is in 12 hour format - then the above code results null
What do I do for this issue ??
bacause you time format is set to 12 hours on ipad device, the date string is in 24 hours format look at string #"14:37:00".
use
dateFormatter.dateFormat = #"hh:mm:ss";
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
Reference
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.
Use hh instead of HH.
hh usually is 12 hour time while HH is usually 24 hour time.
A good official reference for this, as linked by Apple themselves, is here. Another good table is here, as mentioned by Zaph.
Related
trying to convert NSString to NSDate
NSString *startDateForCal='09-03-2016 08:00:00 AM'
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"dd-MM-yyyy HH:mm:ss a"];
NSDate * EventStart = [dateFormatter dateFromString: startDateForCal];
But getting mixed result as "2016-03-09 18:30:00 +0000" returning date as correct but time didn't match with string It would be welcome any suggestions
Thank you
it should be
[dateFormatter setDateFormat: #"dd-MM-yyyy hh:mm:ss a"];
i.e. no capital HH for hour. HH return the Hour in 24 format
For more details Click now
and regarding the error in date,
since you have set the time format wrong( as HH instead of hh), it took the time as 12:00 AM and showed it in GMT timezone(IST - 5 and half hours, so in your case 12:00 AM - 5:30 = 18:30 of previous day), i guess you haven't set the locale properly.
setting the locale(in swift),
dateFormatter.locale = NSLocale(localeIdentifier:"en_IN")
Your question makes no sense. Your goal was to obtain an NSDate from an NSString. You did that, and you did it successfully.
How you discover what that NSDate is, is a completely different matter. It is represented to you by default (in the console) in a certain time zone and format. But that is irrelevant; it is still the same date, and it is the correct date.
I want to get the current time and parse it to an String in 24 hours format, for example 13:30.
This is my code:
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:#"HH:mm"];
NSString *timeString = [formatter stringFromDate:date];
It works on the iOS simulator, but on my iPad I get 3:30 PM.
I have played around with different formats but still the same result.
Any idea why?
Thanks in advance.
The issue is the user's local is set to a 12 hour format. Set the local to en_US_POSIX to over-ride the user's setting.
dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
You need to explicitly set the locale to one with a 24 hour time.
See NSDateFormatter Locale for details on how to set the locale for the NSDateFormatter.
Note the answer by #mtb to that question. It explains how to not use a locale.
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.
This strange issue appeared with ios 8 release. Here is sample code:
NSDate * date = [NSDate dateWithTimeIntervalSince1970:1414785600];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ru_RU"]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Moscow"]];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setDateFormat:#"MM"];
NSString * month = [dateFormatter stringFromDate: date];
Date is 2014-11-01 00:00:00 MSK (or 2014-10-31 20:00:00 +0000)
Running ios 7, month value is 11. But on ios 8 it is 10.
Any ideas what's wrong?
Thanks.
PS. Checking Asia/Muscat timezone right now (+4 like MSK). Everything is OK, month is 11.
[timezone secondsFromGMTForDate:date] returns 14400 at iOs7 and 10800 at iOs8 for the given date. It should reflect the changes done (again) by the russian government http://www.timeanddate.com/time/change/russia/moscow which iOs7 isn't aware of yet.
I am fetching data from the server and part of that data is time. Time is stored in UTC in my DB so what I'm returning is also in UTC. For example: 2014-05-22 05:12:40
Further, I am using the DateTools to show the time difference on the device like X hours ago or X minutes ago etc....
The problem is that when the UTC time coming from the server is compared to the local time there is a huge difference. Something that should say X seconds ago says X hours ago because of the time difference.
Question
How can I convert the date coming from the server to the local time zone set on the device?
This is what I'm doing right now:
#date_formatter = NSDateFormatter.alloc.init
#date_formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
#date_from_server = "2014-05-22 05:12:40"
#date = #date_formatter.dateFromString(#date_from_server)
#time.text = #date.shortTimeAgoSinceNow
I don't know how it is done in ruby motion but in Objective-C, it is done as follows :
1.) Create an instance of NSDateFormatter class
2.) Set a specific date format string of it, and also you set the specific time zone of it
3.) Get a date from the incoming string via the dateformatter instance
4.) Change the time zoneof the date formatter instance to the local time zone
5.) Convert the date obtained previously to the new time zone .
In Objective-C
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:incomingDateFormat];
[df setTimeZone:incomingTimeZone]; // Can be set to #"UTC"
NSDate *date = [df dateFromString:incomingDateString];
[df setDateFormat:newDateFormat];
[df setTimeZone:newTimeZone]; //// Can be set to [NSTimeZone localTimeZone]
NSString *newDateStr = [df stringFromDate:date];
I believe the same can be done in Rubymotion too.
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
formater.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSString *datestr = #"2014-05-22 05:12:40";
formater.timeZone = [NSTimeZone localTimeZone];
NSDate *date = [[formater dateFromString:datestr] dateByAddingTimeInterval:formater.timeZone.secondsFromGMT];
NSDateFormatter *df=[[[NSDateFormatter alloc] init] autorelease];
// Set the date format according to your needs
[df setTimeZone:[NSTimeZone timeZoneWithName:#"America/Toronto"]];
//[df setDateFormat:#"MM/dd/YYYY HH:mm "] // for 24 hour format
[df setDateFormat:#"YYYY-MM-dd HH:mm:ss"];// for 12 hour format
Try this