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.
Related
I need to convert a 12 Hour time format to 24 Hour format. Specifically the PM part of a date, as the API I communicate with only accepts 24-hour format.
Example:
02:00 PM needs to be converted to 14:00.
08:30 PM needs to be converted to 20:30.
I've tried several approaches and probably been close, but I can't seem to get it quite right.
For that you need to use NSDateFormatter and convert the time format to 24 Hours format.
NSString *time12Hours = #"02:00 PM";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSDate *date = [formatter dateFromString:time12Hours];
[formatter setDateFormat:#"HH:mm"];
NSString *time24Hours = [formatter stringFromDate:date];
iPhone format strings are in Unicode format. Behind the link is a table explaining what all the letters above mean so you can build your own.
You can try this website for find your format string nsdateformatter.com whit NSDateFormatter
I'm converting 12 hour date to 24 here, but it failed to get perfect time.
Here is code :
NSString *dateStr = #"2016-08-12T04:10:14.915Z";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'hh:mm:ss.SSS'Z'"];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [dateFormat dateFromString:dateStr];
and my final date is
2016-08-11 22:40:14 +0000
How ?
The NSDate object that you are getting is GMT. In France GMT -2, if I run your code I have a time of 02:10.
Am I correct assuming your GMT offset is -5:30 ?
NSDate objects don't have time zones; they represent an absolute
moment in time. However, when you ask one for its description (by
printing it in an NSLog, e.g.), it has to pick a time zone. The most
reasonable "default" choice is GMT. If you're not in GMT yourself, the
date will seem to be incorrect, by the amount of your own offset.
You should always use an NSDateFormatter, setting its timezone to
yours, before displaying a date.
Don't trust what NSLog or the debbuger are telling you about a NSDate.
use
NSString dateAsString = [dateFormat stringFromDate:date];
to check your date :)
I'm somewhat confused about how these two NSDate objects seem to know which timezone they are in. I was under the impression that an NSDate object only stored a point in time and no information about the timezone.
I'm creating and logging two dates like this:
NSString* timeString = #"6:04 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
[dateFormatter setDateFormat:#"h:mm a"];
NSDate *time = [dateFormatter dateFromString:timeString];
NSDateFormatter *currentFormatter = [[NSDateFormatter alloc] init];
[currentFormatter setTimeStyle:NSDateFormatterLongStyle];
NSLog(#"%#", [currentFormatter stringFromDate:time]);
NSLog(#"%#", [currentFormatter stringFromDate:[NSDate date]]);
This produces the output:
18:04:00 GMT
11:12:36 BST
How does it know that the first date is GMT and the second is BST?
(It's British Summer Time here. Your mileage may vary in that respect)
You're right, NSDate doesn't have a time zone. Your results don't contradict that, because you're not printing the dates-- you're printing a subset of the date information produced by passing them through a date formatter. Your currentFormatter only returns time of day information, not date information. If you add this line:
[currentFormatter setDateStyle:NSDateFormatterLongStyle];
Then the results will look something like:
January 1, 2000 at 6:04:00 PM MST
September 30, 2015 at 11:14:39 AM MDT
In other words, they show up with different time zones because they're on different dates, and the time zone reflects what's in effect on that date. In my case it's currently MDT but on Jan 1 2000 it would have been MST.
Here's the code:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate* newTime = [dateFormatter dateFromString:#"2011-10-19T12:22:07Z"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"dd-MMM-yyyy HHmm"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
The problem:
I'm in Toronto (EST, GMT-5). My final time SHOULD show 0722, I'm seeing 0822. Inspecting the objects I can see that newTime is '2011-10-19 08:22:07 EDT'. I'm not sure why that happens but it persists onto the finalTime string despite setting the dateFormatter time zone to systemTimeZone. I assume systemTimeZone is EDT then? Any insight into why this happens or how I can correct it would be greatly appreciated.
Thanks in advance.
You're currently in Toronto EST. This time report you are getting back is showing EDT. Which is Eastern Daylight Time. Make sure you subtract an hour for daylight savings time since it's EDT not EST.
I'm out right now so I can't test this code but I think this should work:
NSDate *currentDate = [NSDate date];
NSTimeZone *currentZone = [NSTimeZone localTimeZone];
if ([currentZone isDaylightSavingTimeForDate:currentDate]) {
//adjust the time by 1 hour here
}
A few things:
Don't set the timezone on the date formatter. And then remove the quotes around the Z in the format string. This will allow the date formatter to determine the timezone from the actual date string.
The output of 0822 is correct for your system timezone because you were on day light savings time on October 19th. On that date you were GMT-4, not GMT-5. In Canada, in 2011, DST ended on November 6th.
I am using the below method to convert a NSString to NSDate.
Always when I construct the NSDate from String, the date is one day behind the current day I have provided as part of the input and hour is 18:30:00 +0000. Why this deviation from what I have provided. I was expecting to have the same date what I have provided and hour as 00:00:00 +0000
+(NSDate*)convertStringToNSDate:(NSString*)string withFormat:(NSString*)format{
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
//[dateFormat setTimeZone:[NSTimeZone localTimeZone]];
[dateFormat setDateFormat:format];
NSDate *date = [dateFormat dateFromString:string];
[dateFormat release];
return date;
}
This question comes up quite regularly but I could not find a suitable duplicate (searching on the phone does not help).
NSDate represents a specific point in time. When you log the value of an NSDate it is displayed in GMT, which is 5.5 hours behind your timezone (India, I assume). So the value is correct. If you run that date back through your date formatter you will get the local time of midnight again, since the date formatter is using your local time zone.