I am trying to get NSDate from string but its returning nil.
I tried this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-mm-dd'T'HH:mm:ss'Z'"];
NSDate *date = [dateFormatter dateFromString:#"2010-05-07T10:12:32UTC"];
NSLog(#"Success date=%#",[date description]);
Thanks
Your date format string expects the date to end with a literal Z. But your date ends with the string UTC. There are several ways to fix this.
Change your date format string to #"yyyy-mm-dd'T'HH:mm:ss'UTC'".
Or, change your date string to #"2010-05-07T10:12:32Z".
Or, you could change your date format string to: #"yyyy-mm-dd'T'HH:mm:ssz". This will recognize some common 3-letter time zone names, such as PDT for Pacific Daylight Time. Unfortunately, however, it will not recognize UTC as a time zone name (you’d have to use “GMT”).
The literal 'Z' won't match the string UTC.
Related
I have the following Objective-C code that attempts to generate a string with the current date/time in UTC:
NSDate *currentDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *utc = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:utc];
[dateFormatter setDateFormat:#"YYYY-MM-dd--HH-mm-ss zzz"];
NSString *utcDateString = [dateFormatter stringFromDate:currentDate];
This code almost generates what I want. The resulting value of utcDateString on my system is:
2019-06-17--22-28-13 GMT
However, my desired output includes the string "UTC", not "GMT", as in:
2019-06-17--22-28-13 UTC
Is there a way to get the NSDateFormatter to emit the string "UTC" for the zzz portion of the date format? I'd prefer not to have to resort to leaving the "zzz" off of my date format string, and then manually append a "UTC" onto the resulting date string.
There is no date format specifier that returns UTC.
Since you are hardcoding the UTC timezone for the formatter, then simply hardcode the string UTC in the date format.
Also note that you want yyyy, not YYYY for the year.
[dateFormatter setDateFormat:#"yyyy-MM-dd--HH-mm-ss 'UTC'"];
If you need to handle other timezones and want GMT to appear as UTC, then use zzz (as you already are) and use string replacement on the result to convert GMT to UTC.
One other possible idea to consider is to use some number of X for the timezone specifier. This will give the timezone offset as numbers but if the timezone offset is 0, then it results in Z (for Zulu).
This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Closed 7 years ago.
Important update: Of you hover over a NSDATE with your mouse the degugger will convert the NSDate to your local timezone you have set on you Mac, but if you do a NSLOG you will notice that the NSDate is using the timezone that you assigned to the its respective formatter.
If you want to see in the xcode debugger what the NSDate is for the timezone you are working with go to your date/time settings for you Mac OS and change the Timezone to the one you are testing.
I require a NSDate to be created from the date I pass in, but currently it is set to the the day before I pass in:
NSString *dateStr = #"2015-08-09";
NSDateFormatter *myformatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSDate *date = [formatter dateFromString:dateStr];
The code above returns: an NSDate set to '2015-08-08 12:00:00 +0000'
I need an NSDate object set to the datStr I pass in.
This perhaps, from this:
Convert NSDate to NSString with NSDateFormatter with TimeZone without GMT Time Modifier
leave the 'z' lowercase for named timezone, i.e. PST and uppercase 'Z' for -0800
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMMM dd, yyyy (EEEE) HH:mm:ss z Z"];
NSDate *now = [NSDate date];
NSString *nsstr = [format stringFromDate:now];
Also, you date should be more robust, like if you want to pass in the current day that your are passing in and it's one day off, then just add a day. The problem, it seems is that the date is returning the correct date which is the end of the last day, add 1 second or a millisecond and it'll probably be corrected, or just hack attack this and add 1 day to the days you are passing in. Be smart! Sometimes you just have to add 1 day to fix stuff and move on.
I have written the following method:
- (NSDate *)stringToDate:(NSString *)dateString
{
// Convert string to date
NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithName:#"America/New_York"];
[dateFormat setDateFormat:#"M/d/yyyy HH:mm:ss"];
[dateFormat setTimeZone:tz];
NSDate * date = [dateFormat dateFromString:[NSString stringWithFormat:#"%# 00:00:00", dateString]];
return dateOnly;
}
When I call this with just a date such as 11/1/2013 or 11/13/2013 I get 2013-11-01 04:00:00 +0000 and 2013-11-13 05:00:00 +0000.
If I set a breakpoint on the return the date appears right, but if I break at in the calling function after this call, the date is returned with the time.
How come my time is not always 0. Can anyway tell me what is wrong in my function?
Thank you
UPDATE:
The input string is as follows: 11/1/2013 and 11/13/2013
NSDate is a point in time. It will always have a time component.
And if not printed as a string form a NSDateFormatter, the Date and time will always be the one of UTC/GMT.
The format and the date string must fit.
NSString *dateString = #"11/1/2013";
[dateFormat setDateFormat:#"M/d/yyyy"];
The one hour apart comes from the Daylight saving time. Till November, 3rd 2013 New York has Summer time. http://www.timeanddate.com/worldclock/clockchange.html?n=179
Ok, so can I ignore that? I am trying to compare NSDates when I do my comparison fails because of the time part
You should create dates with with a time during the day — i.e. noon — to be save of DST mess and compare those. Use NSComponents for that.
A must-see for any iOS/Cocoa-Developer: the fantastic WWDC 2011 Video "Session 117 - Performing Calendar Calculations".
I want to search my Array of Dictionary for particular date.Ex I want to search my array of dictionary for date "16 Jan 2012" which is in string format but my dictionary item in array contains date and time, say "16 Jan 2012 somehours:somemins:somesecs".I am converting string format date in NSDate format but I am getting date as 2012-01-15 18:30:00 +0000 instead of 2012-01-16.I am using NSPredicate to search for the date which convert date into seconds as follows "Date == CAST(348345000.000000, "NSDate")" and compare so even though my records contain date as "16 Jan 2012 somehours:somemins:somesecs" it will not satisfied the criteria.I want that the records/array containing date as "16 Jan 2012 somehours:somemins:somesecs" should satisfied the search criteria.Please can anyone know how to achieve this?
Thanks in advance!
To rid yourself of NSDateFormatter's automatic adjustment for your time zone use something like this.
dateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
To elaborate; "16 Jan 2012" Contains no hour, minute, or seconds value, so when parsing it with an NSDateFormatter one would expect an NSDate with a description of "2012-01-16 00:00:00 +000" But that's not what you're getting because the date formatter is adjusting for the 5:30 Hours between GMT and India?(Assuming based on :30 differential). By setting the date formatter's time zone explicitly you avoid this problem.
You need to use NSDateFormatters to convert the date string from one format to another. You can have an inputDateFormatter where you use dateFromString to give you an NSDate, and then feed this into an outputDateFormatter which gives your desired string.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd MMM yyyy"];
NSString *dateInFormatYouWant = [dateFormatter stringFromDate:yourDate]
I'm not sure if there is a more efficient way of doing it than this.
I'm trying to use only a NSDateFormatter to format string representation of a date/time to an NSDate object. The issue is that I can't figure out how to allow the ordinal suffixes in the format.
So lets say I have a string
"Wednesday, August 11th 2010 8:00 PM"
What one line NSDate dateFormat should I use?
Like "EEEE, MMM dd'th' yyyy h:mm a" would work, but that will only work for ordinal days ending in 'th', whereas i need a single format that will allow for 1st, 2nd, 3rd, 4th 5th etc.
It seems like a wildcard character in the format string to accomplish this. I've tried a few things like: % * ?
This is apparently not possible with the NSDateFormatter.
You want to use an NSDateFormatter like so:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle: NSDateFormatterLongStyle];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
Either NSDateFormatterLongStyle or NSDateFormatterFullStyle should get you the results you're looking for.