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).
Related
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.
This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 8 years ago.
I have some trouble to convert this string :
#"15/08/2014 08:30"
Into an NSDate, here my code :
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeZone:[NSTimeZone localTimeZone]];
[formatter setDateFormat:#"dd/MM/yyyy HH:mm"];
When I print out [formatter dateFromString:fullDatesString]; (fullDatesString is my string) the result is :
2014-08-15 06:30:00 +0000
Why my time is less than 2 hours between my string and my NSDate object ?
When you print the description of the NSDate, it prints in in GMT (+0000) regardless of what timezone you or the NSDate is in.
If you convert that time to your timezone (GMT + 2 I assume), then the time is correct.
This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 8 years ago.
I don't why i'm getting one date less, when I'm converting a string from a date, i'm getting one date less, e.g. when i'm converting 18/06/2014, i'm getting 2014-06-17, Any idea why this problem, my codes are:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
This is what I'm getting wholly from the log: 2014-06-17 20:00:00 +0000
You will have to take the timezone into account. Your current timezone seems to be ahead of GMT. If you print the entire date with say a time stamp, then you will get the difference. So i suggest you add the timezone to the NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy"];
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *date = [dateFormatter dateFromString:#"18/06/2014"];
NSLog(#"Date : %#", date);
This question already has answers here:
NSString to NSDate
(5 answers)
Closed 9 years ago.
I'm trying to get the date "30/06/2013" from the button:
I tried this code but I got in the log: 2013-01-04 22:00:00 +0000:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/YYYY"];
NSDate *date = [formatter dateFromString:dateBtn.titleLabel.text];
NSLog(#"date %#",date);
and in the app i got this date "05/01/2013":
I also tried this code but it didn't work:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"he_IL"];
[dateFormat setLocale:locale];
[dateFormat setDateFormat:#"dd/MM/YYYY"];
NSTimeInterval interval = 5 * 60 * 60;
NSDate *date1 = [dateFormat dateFromString:dateBtn.titleLabel.text];
date1 = [date1 dateByAddingTimeInterval:interval];
It is important to note I use locale Hebrew.
Have a look at Apple's official documentation on date formatters. Please note how the format differs slightly across different platforms and versions.
Also note the difference between yyyy and YYYY.
set date format to "dd/MM/yyyy"
NSString *originalDate = #"30/06/2013";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"dd/MM/yyyy"];
NSDate *date = [formatter dateFromString:originalDate];
NSLog(#"date %#",date);
The explanation from #fzwo apple link
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
format a NSDate to DDMMYYYY?
Want NSDateformatter for January,2000 or January 2000
I need date and time in following format. How can I get ..
Dec 17,2012 5:30 AM
You can always google for these kind of issues
Try to look at NSDateFormatter Class,
The format you are looking for is something like:
[dateFormatter setDateFormat:#"MMM dd,yyy hh:mm:ss a"]; //dateFormatter is an object of class NSDateFormatter
NSDate *selected_Date = //Your Date object here;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MMM dd,yyy hh:mm:ss a"];
NSString* strDateObj [NSString stringWithFormat:#"%#",[dateFormat stringFromDate:selected_Date]];
NSLog(#"Date %#",)