CST timezone add one day from november 3rd in iphone? - iphone

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 *'

Related

iOS - Converting string to non-en-GB locale NSDate returns nil [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
NSString to NSDate
I have a date in format Mon Jan 14 14:00:00 CET 2013 I try to convert it to NSDate:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];
but it doesn't work and my date is nil
Input data is in en-GB locale, my device's locale is nb-NO
Any suggestions?
You're missing day in your format:
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
If it was not a typo, then next thing is to set proper locale so formatter will recognise CET timezone, for example en-GB will fix that:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
// that will fix the problem with not recognized CET timezone
[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en-GB"]];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 CET 2013"
NSDate *date = [df dateFromString:dateString];
Try to use this function
- (NSDate*) dateFromString:(NSString*)aStr
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"] autorelease]];
//[dateFormatter setDateFormat:#"YYYY-MM-dd HH:mm:ss a"];
[dateFormatter setDateFormat:#"MM/dd/yyyy HH:mm:ss a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSLog(#"%#", aStr);
NSDate *aDate = [dateFormatter dateFromString:aStr];
[dateFormatter release];
return aDate;
}
I hope this will helps u.
I think your Time Zone is wrong. Just use this code , it will work Perfectly :
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz y"];
NSString *dateString = #"Mon Jan 14 14:00:00 EDT 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"date :: %#",date);
It will log Output as :
date :: 2013-01-14 18:00:00 +0000
EDIT :
I found Something for you : NSDateFormatter doesn't parse some timezones
You can solve this by using en_GB Locale , as stated : "These abbreviations do still work with the en_GB locale" in Working with Date and Time in Cocoa .
CET is not recognised
Try this :-
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss yyyy"];
NSString *dateString = #"Mon Jan 14 14:00:00 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"%#",date);
Hope it helps you
Simply "CET" is not a recognized time zone by NSDateFormatter.
Also the date/tine is over specified, best to not try include the day or week (Mon).
Here is an example that demonstrates working code with a recognized timezone:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"EEE MMM dd HH:mm:ss zzz yyyy"];
NSString *dateString = #"Mon Jan 14 14:00:00 EST 2013";
NSDate *date = [df dateFromString:dateString];
NSLog(#"date: %#", date);
NSLog output
date: 2013-01-14 19:00:00 +0000
NSLog(#"abbreviationDictionary: %#", [NSTimeZone abbreviationDictionary]);
does show
CET = "Europe/Paris";
so this looks like an Apple bug in NSDateFormatter.
Report the bug at: Apple Bug Reporter
You can use:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
If you still want to custom your date format try this one:
yyyy-MM-dd HH:mm:ss ZZZ
Because can't invent your own formatted string syntax and expect it to work; you need to actually use a documented format as the documentation points it out : Formatters in OS X v10.8 and iOS 6.0 use version tr35-25.
-> https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
If you are curious: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

NSDateFormatter Adding 11 Extra Minutes when formatting

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.

Still having NSDateFormatter result issues even with NSTimezone properly set, why?

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

NSDate format issue

Here is the code from the nsdate formatter... for some reason the value dateSelected is incorrect... instead of "April 30 2011 7:55PM" it returns 2011-05-01 02:55... any idea what am i doing wrong?
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:#"h:mm a"];
objEventInsert.eventtime = [outputFormatter stringFromDate:self.datePicker.date];
NSLog(#"%#",objEventInsert.eventtime);
NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
[dateForm setDateFormat:#"LLLL d y h:mm a"];
NSDate *dateSelected = [dateForm dateFromString:[NSString stringWithFormat:#"%# %#",objEventInsert.eventstartdate,objEventInsert.eventtime]];
NSLog(#"%#",objEventInsert.eventstartdate);
objEventInsert.date = dateSelected;
NSLog(#"%#",objEventInsert.date);
NSLog response...
2011-04-30 19:54:14.264 APP[24017:207] 7:55 PM
2011-04-30 19:54:16.216 APP[24017:207] April 30 2011
2011-04-30 19:54:17.654 APP[24017:207] 2011-05-01 02:55:00 +0000
That's the correct UTC time. You'll need to set the locale/timezone to get the local time, i.e. 7:55.
See these answer examples
Inconsistent behaviour with NSDateFormatter on two different devices
NSDate dateFromString, how to parse 'around' UTC, GMT and User locale?
Your problem is that you create a new NSDate again and you just initiate it through a string. So either your should create a string in your last step or your need to reuse the NSDateFormatter.
NSString *dateSelected = [NSString stringWithFormat:#"%# %#",objEventInsert.eventstartdate,objEventInsert.eventtime];
NSLog(#"%#", dateSelected);
Note: can use appendStringByFormat to make your code less verbose.

How can I get the date but not time from NSDate

I need the date as a string but not the time and it has to be localized.
So for example USA should be Sep 25 2009 but for New Zealand it would be 25 Sep 2009.
I can get the date into a string by specifying the format "MMM dd YYYY" but It's not localized.
Any ideas?
[NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterMediumStyle
timeStyle:0]
The key is to send setTimeStyle:kCFDateFormatterNoStyle to the dateFormatter. That sets the dateFormatter so that it will only format the date and not output the time:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale: [NSLocale currentLocale]];
[dateFormatter setDateStyle:kCFDateFormatterShortStyle]; // or whichever style...
[dateFormatter setTimeStyle:kCFDateFormatterNoStyle];
NSString* dateString = [dateFormatter stringFromDate: [NSDate date]];
This gives me the following output:
12/18/09