I seem to be adding +11 minutes to my desired date values when I am converting them using NSDateFormatter.
The startDate and endDate as NSDates on MyObject, printed to the console:
START DATE: 2011-11-28 18:00:00 +0000
END DATE: 2011-11-28 20:00:00 +0000
My NSDateFormatter:
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:#"hh:MM a"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
Setting the dateRangeString:
NSString *dateRangeString = [NSString stringWithFormat:#"%# - %#",
[formatter stringFromDate:self.startDate],
[formatter stringFromDate:self.endDate]];
The wrong resulting dateRangeString:
Date Range: 18:11 PM - 20:11 PM
Why is it adding an extra 11 minutes to the times? What am I doing incorrect in my conversion? Am I forgetting something silly? SO, please help my sanity!
[formatter setDateFormat:#"HH:mm a"];
MM
is for months.
Formatting NSDate into particular styles for both year, month, day, and hour, minute, seconds
Because it's November, the 11th month.
Related
I tried to add one day from November 3rd with timezone like dallas, but i can't do it.
NSDate *intime = #"2013-11-03 05:00:00 +0000";
NSDate *nextday= [NSDate dateWithTimeInterval:24*3600 sinceDate:intime];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MM/dd/yyyy"];
NSString *convert=[formatter stringFromDate:nextday];
NSLog(#"convert:%#",convert);
Nslog values:
intime: 2013-11-03 05:00:00 +0000
nextday: 2013-11-04 05:00:00 +0000
convert: 11/03/2013 23:00:00 // but I need this 11/04/2013.
Please let me know, how to do that,
Thanks in advance
The problem is with the intime - you have to use formatter to initialize a NSDate from string. So my tested example would be:
// NSDate *intime = #"2013-11-03 05:00:00 +0000"; // wrong
NSString *str =#"2013-11-03 05:00:00 +0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss ZZZ"];
NSDate *intime = [formatter dateFromString:str];
NSDate *nextday= [NSDate dateWithTimeInterval:24*3600 sinceDate:intime];
NSDateFormatter *formatter2=[[NSDateFormatter alloc]init];
[formatter2 setDateFormat:#"MM/dd/yyyy"];
NSString *convert=[formatter2 stringFromDate:nextday];
NSLog(#"%#, %#, %#", intime, nextday, convert);
The output is:
2013-08-08 15:03:23.762 2013-11-03 05:00:00 +0000, 2013-11-04 05:00:00 +0000, 11/04/2013
One small comment: by default XCode writes out these kind of problems with the code when showing issues is switched on (Preferences->General->Show live issues).
Eg. in this case XCode warns you with this issue: Incompatible pointer types initializing 'NSDate *__strong' with an expression of type 'NSString *'
One simple thing on conversion from NSString to NSDate. How can I convert Mon, 27 August 2012 01:30 AM to NSDate in this same format. I tried with NSDateFormatter. But I am not getting it in this required format. Can anyone help? This is what I tried.
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEEE,dd MM yyyy HH:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
NSDateFormatter is to specify the format that will appear in the date-string when extracting string from date or the format that is in the date-string when extracting date from string
So whenever you extract NSDate from a NSString, NSDate is always obtained in default date format(eg 2012-08-27 00:30:00 +0000)... only the when you extract NSString from NSDate, NSString can be obtained in desired(custom) format that you set in NSDateFormatter.
I hope this will help you sure!
NSDateFormatter *dateformater=[[[NSDateFormatter alloc] init]autorelease];
[dateformater setDateFormat:#"EEEE,dd MMMM yyyy HH:mm a"];
NSDate *todayTmp=[NSDate date];
NSString *conversionDate=[dateformater stringFromDate:todayTmp];
Note : (Upper case) HH for 24h time format, (Lower case) hh for 12h time format
NSString *myDateAsAStringValue = #"Mon, 27 August 2012 01:30 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSDate *myDate = [[NSDate alloc]init];
myDate = [df dateFromString:myDateAsAStringValue];
[df release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, dd MMM yyyy HH:mm a"];
NSString *strDate = [dateFormatter stringFromDate:myDate];
NSLog(#"%#", strDate);
[dateFormatter release];
please use below code
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
your formatter is wrong
check this one
NSLog will return NSDate in a fixed format, i guess.
If we need Date in different format, we should have to format it via NSDateFormatter and get it as NSString.
Just a guess.
Don't forget to set the correct locale! If your device does not use an english locale NSDateFormatter can have problems to convert Mon and August into useful information because Mon is not the correct abbreviation for Monday in your language. For example in Germany the correct three letter abbreviation for Monday is Mon..
If you parse dates that have words in it you have to set the correct locale.
This should work:
NSDateFormatter *df=[[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE,dd MMMM yyyy hh:mm a"];
NSLocale *posixLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[df setLocale:posixLocale];
NSDate *date1 = [df dateFromString:#"Mon,27 August 2012 01:30 AM"];
NSLog(#"%#",date1);
EEE is the dateformatter code for a three letter weekday abbreviation.
hh is the dateformatter code for Hours between 1 and 12. HH means 0-23
MMMM is the full month, MM would be the numeric value (= 08) of the month.
The result is still a day before, I'm just asking myself why, because the NSTimeZone is properly set and is the right one for my country (italy, rome)
here's my stub of code, any ideas?
NSString *dateString = #"03/07/2008";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setFormatterBehavior:[NSDateFormatter defaultFormatterBehavior]];
[formatter setDateFormat:#"dd/MM/yyyy"];
[formatter setLocale:[NSLocale currentLocale]];
[formatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *dateFromString = [formatter dateFromString:dateString];
[formatter release];
the result in dateFromString is this 2008-07-02 22:00:00 +0000.
I've looked for other solutions but the common answer was to set the timezone correctly, in my case it is set properly but the problem still remains.
That is correct because by default the NSDate will return a UTC date in its description +0000. You are a couple of hours ahead of UTC so you get 22:00:00 for the day prior. I am -5 and my result UTC is 2008-07-03 04:00:00 +0000 (DST). The date is correct, it is just being displayed in UTC, if you are trying to display it correctly somewhere just use the date formatter to get a string again.
...
NSDate *dateFromString = [formatter dateFromString:dateString];
NSString *stringFromDate = [formatter stringFromDate:dateFromString];
[formatter release];
NSLog(#"%# : %#", dateFromString, stringFromDate);
2008-07-03 04:00:00 +0000 : 03/07/2008
I have implemented one iphone application in which I want to convert NSDate to NSString but in german format.
Can you give me some idea about that.
I am using below code.
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[eventInfo valueForKey:#"startdat"] intValue]];
//2011-05-01 21:04:00 +0000(I am geeting this date)
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
NSLocale *nl_NL = [[NSLocale alloc] initWithLocaleIdentifier:#"de_DE"];
[formatter1 setDateFormat:#"E,dd MMM yyyy"];
[formatter1 setLocale:nl_NL];
NSString *stringFromDate1 = [formatter1 stringFromDate:date];
[formatter1 release];
[nl_NL release];
//I am getting stringFromDate1 = "Mo.,02 Mai 2011" value.(wrong output)
Please give me idea
Use for example
[formatter1 setDateStyle:NSDateFormatterLongStyle];
instead of setDateFormat:.
The problem is that the time zone is taken into account when the NSDateFormatter is formatting the date. If you want the NSDateFormatter to format the exact same date as the NSLog'd version, you need to explicitly set the time zone of the formatter.
[formatter1 setDateFormat:#"E,dd MMM yyyy"];
[formatter1 setLocale:nl_NL];
[formatter1 setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"GMT"]];
I am not sure (from your question), what is your expected output. But assuming that you are looking for the full day of week description, try this date format:
[dateFormatter setDateFormat:#"EEEE, dd MMM yyyy"];
It gives the output:
Sonntag, 01 Mai 2011 (using de_DE locale)
In general, the number of characters determine the size of date field:
eg. Input date = 2011-05-01 Sunday
1-character = 1-digit/character number or word (if number/word can't be 1 character long then abbreviation or fullname is displayed).
[dateFormatter setDateFormat:#"E, d M y"]; // Sun, 1 5 2011
2-character = 2-digit/character number or word (if number/word can't be 2 character long then abbreviation is displayed).
[dateFormatter setDateFormat:#"EE, dd MM yy"]; // Sun, 01 05 11
3-character = 3-digit/character number or word, or abbreviation (generally).
[dateFormatter setDateFormat:#"EEE, ddd MMM yyy"]; // Sun, 001 May 2011
4-character = full name (generally).
[dateFormatter setDateFormat:#"EEEE, dddd MMMM yyyy"]; // Sunday, 0001 May 2011
Here's the weird part though, if you specify 5 E's, you get an rather unexpected output:
[dateFormatter setDateFormat:#"EEEEE, ddddd MMMMM yyyyy"]; // S, 00001 M 2011
For date formatting, I find the the following reference table very useful.
http://www.unicode.org/reports/tr35/#Date_Field_Symbol_Table
Good luck
I am having a problem with my datetime field getting changed by the time zone.
The incoming datetime is -- 2010-12-28 19:10:00
only when I use get date from string it comes out as -- 2010-12-29 00:10:00 +0000
I am doing
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[[dateFormatter locale] localeIdentifier];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter setTimeStyle:NSDateFormatterLongStyle];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
p.date = [dateFormatter dateFromString:[values objectAtIndex:6]];
It's like it it applies GMT and adds 5 hours to my date time. How can I stop this from happening.
thanks
Cheryl
Assuming your local time is GMT-5, this is perfectly fine. 2010-12-29 00:10:00 +0000 and 2010-12-28 19:10:00 -0500 both identify exactly the same point in time. And that is all an NSDate represents: a single point in time. The time zone in which you display it is determined when you create another date formatter to convert the date back to a string for display.