I am aware that this question is asked too many times but none matched my requirement.
I have "8-8-2015 12:00:00 AM" in NSString named strEventTimeBegin. How do I convert this NSString to NSDate? Here's what I tried but returns null.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
//[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSDate *startEventDate = [dateFormatter dateFromString:strEventTimeBegin];
From memory, but this should work:
[dateFormatter setDateFormat:#"d-M-yyyy hh:mm:ss a"];
Just to explain, in your example;
The day and the year are backwards.
You're using 2 digit days and
months (It's looking for 08-08-2015).
You're missing the period
(AM/PM) identifier.
Related
Every time I try to pass a date string to the dateFromString function I get nil. I haven't read anywhere that things have changed since the iOS 7 update, but I am current on updates if that makes a difference on whether or not this still works the same way.
This is the code I'm using to create the date from string:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setLocale:[NSLocale systemLocale]];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
[dateFormat setFormatterBehavior:NSDateFormatterBehaviorDefault];
and my date string is #"2015-08-22 13:00:00"
NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:#"2015-08-22 13:00:00"];
return date;
The last line returns nil.
You need to change:
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
With:
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Since hh refers to the 12 hour clock and HH refers to the 24 hour clock.
hh represents hours in 12-hour-mode and 13 is out of range.
Replace hh with HH for 24-hour-mode
I had exactly the same problem last week and I solved it with this code:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateFromString.doubleValue];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSString *stringDate = [dateFormatter stringFromDate:date];
Here you have a string named dateFromString because this is the object type for date which I recieve from server. This string is converted in double value and the date is calculated with time interval.
I suppose you need this date to be shown in a label or text view or another UI element like this, and just because of that I converted the date into NSString to be easy to use it. This is what I have in stringDate.
Let me know if I have to help you more.
smaller case letters for 12 hour clock and upper case for 24-hr. Change hh with HH.
I googled so many times, but I am not satisfied with given answer. Please anyone can give correct answer what I need.
This is the retrieve date string from DB : 2015-05-27 10:19 (yyyy-MM-dd HH:mm)
I want to convert into NSDate.
My code is like Below:
NSString *date_str = #"2015-05-27 10:19";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:date_str];
NSLog(#"date == %#",date);
But output is : date == 2015-05-27 04:49:00 +0000
Its showing 04:49:00 time , but my retrieve time is 10:19.
How can i retrieve perfect time from DB.
Please help out me..
Just convert your date to GMT time like this:-
NSString *date_str = #"2015-05-27 10:19";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
[dateFormat setTimeZone:gmt];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *date = [dateFormat dateFromString:date_str];
And the output is :-
date == 2015-05-27 10:19:00 +0000
And to get the date without +0000, you can store it in NSString directly:-
NSString *s = [dateFormat stringFromDate:date];
Output :-
2015-05-27 10:19
You are facing the timezone issue with conversion, because server time and local timezone may have difference. So handle this you need to set the timezone to your dateformatter like
[dateFormater setTimeZone:[NSTimeZone timeZoneWithName:#"GMT"]];
This question already has answers here:
Convert NSDate to NSString
(19 answers)
Closed 8 years ago.
I have an NSDate Object which is in 12 hour format like 2014-09-16 04:40:05 pm +0000.
I want to convert this into 24 hour format and want to get back an NSDate object like 2014-09-16 16:40:05 +0000.
Can some one guide me in doing that.
So i want some method like :
-(NSDate *) get24HourFormat:(NSDate *) date{
return date object;
}
Simple use this:
NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
dateformate.dateFormat = #"HH:mm a"; // Date formater
NSString *timeString = [dateformate stringFromDate:[NSDate date]]; // Convert date to string
NSLog(#"timeString :%#",timeString);
Logic: Simply convert date format hh:mm to HH:mm
There seems to be a deep misunderstanding here what NSDate is and does.
An NSDate object is a point in time in UTC. It doesn't have a time zone. It doesn't have a format. It has nothing. You can't change its format, it doesn't even make any sense.
What you can do is to use an NSDateFormatter to convert the NSDate to a string. By default, NSDateFormatter uses your local time zone, which means for most people that the result will be different from the result that NSLog would show for an NSDate. In the NSDateFormatter, you can use whatever settings you want.
Usually you would respect how the user set up his date formatting and not change it for anything that is visible to the user. As a user, if I had set up my device to show days in 12 hour format, I'd be very annoyed if your application worked differently.
try this..
-(NSString *)changeformate_string24hr:(NSString *)date
{
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
NSDate* wakeTime = [df dateFromString:date];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
return [df stringFromDate:wakeTime];
}
-(NSString *)changeformate_string12hr:(NSString *)date
{
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:#"MM/dd/yyyy HH:mm:ss"];
NSDate* wakeTime = [df dateFromString:date];
[df setDateFormat:#"MM/dd/yyyy hh:mm:ss a"];
return [df stringFromDate:wakeTime];
}
I found this weird issue, when converting from string to a NSDate. The final date is wrong by exactly 2 or 3 hours (possible an integer number of hours). My data has an excellent quality as it was picked from other ios devices programmatically. For example when I try to convert:
"2014-05-23 03:14:04 a.m. +0000"
I get:
2014-05-23 00:14:04 +0000
or, when converting:
"2014-05-23 02:49:30 a.m. +0000"
I get:
2014-05-23 00:49:30 +0000
The date format is in Spanish and therefore my code is:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss a ZZZ"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"es"]];
[dateFormatter setAMSymbol:#"a.m."]; // default AM symbol for spanish is a.m.
[dateFormatter setPMSymbol:#"p.m."]; // default PM symbol for spanish is p.m.
// Set the date format for the input string
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss a ZZZ"];
newEvent.time = [dateFormatter dateFromString:[timeArray objectAtIndex:i]];
according to:
https://stackoverflow.com/a/13757666/2394901
but with a modification for a.m. and p.m. instead of AM and PM because that way the answer is nil.
UPDATE:
This issue is not due to time zone difference as suggested below. Instead the proper solution is:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"es"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ssa ZZZ"];
newEvent.time = [dateFormatter dateFromString:[timeArray objectAtIndex:i]];
**the problem is the capital letters HH, should be hh.
Anybody knows when should letters must by capitalized?
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).