How would I convert this string recentUpdate to an NSdate? My recentUpdate outputs this 10/05/2014 06:43:PM and when a converted it to NSDate, it always results null.
Can someone please tell me what I am doing wrong? Any tips or suggestions are appreciated. My question is a little more specific compare to the other questions on stackoverflow so this isn't a duplicate question.
NSString *recentUpdate = sharedData.mUser.mRecentUpdateTime;
NSDate *date = [NSDate dateFromString:recentUpdate usingFormat:#"MM-dd-yyyy HH:mm:ss"];
Your format does not match the string you gave:
MM-dd-yyyy HH:mm:ss
10/05/2014 06:43:PM
^ ^ ^^
Here and here
If you are converting any string to NSDate, make sure your string format should be same as the format you are passing in date-formatter.
Example -
for string like 10/05/2014 06:43:PM, your date format should be - MM/dd/yyyy HH:mm:a
for string like 10-05-2014 06:43 PM, your date format should be - MM-dd-yyyy HH:mm a
otherwise result will be null .
Related
I'm trying to make this line work:
MyDateTime = DateTime.ParseExact("2/8/2013 11:59:00 AM", "yyyy-mm-dd hh:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo)
I'm getting date/time strings from spreadsheets in the above format, and I can't control that. There's a ton of help online, including this site, about converting strings to dates, and I've tried them all, but I keep getting this error :
"System.FormatException: String was not recognized as a valid DateTime."
I"m just about ready to write my own custom parser, but that doesn't seem very elegant. Is there some built-in way to convert a string like mine into the date/time format I need?
Thanks for any help.
Your format string is wrong. You're entering a date in d/M/yyyy hh:mm:ss tt format, but telling it to expect a date in yyyy-mm-dd hh:mm:ss format. The two do not match exactly, so DateTime.ParseExact is quite rightly throwing an exception at you.
Try:
MyDateTime = DateTime.ParseExact("2/8/2013 11:59:00 AM", "d/M/yyyy hh:mm:ss tt", System.Globalization.DateTimeFormatInfo.InvariantInfo)
This tells it to expect the following characters:
Integer from 1 through 31 (depending on the length of the month)
/ character
Integer from 1 through 12
/ character
4 digit year
Space
Integer from 1 through 12
: character
Integer from 00 through 59
: character
Integer from 00 through 59
Space
Two character meridian specifier ("AM" or "PM")
For more info on the datetime format strings, check out this MSDN page
I think you need to change the string format to match what you are passing in. You seem to be passing in something more like this:
"d/M/yyyy hh:mm:ss"
Give that a try and see how it works. Note that you need to use MM for months--'mm' is used for minutes.
In my application i get from a server some dates.
Some of the dates are in this format : "MMM dd, yyyy hh:mm:ss a" and some others in this : "yyyy-MM-dd HH:mm:ss Z" . I get the dates as strings from the server in one of the above formats. However i want all my dates to be in the first format. So how can i compare if the date is on the second format so i can change it? I know how to change formats , i just dont know how to compare a date with a specific format.
Thanks!
If you know the format that any given returned string is in then you could explicity set the format for each iteration of getting a date and then convert it to whatever format you need. Something like this example;
NSString *myString1 = #"01-30-2012";
NSString *myString2 = #"30-01-2012";
// Now convert the string to a date object
NSDateFormatter *myDateFormat = [[NSDateFormatter alloc] init];
[myDateFormat setDateFormat:#"dd-MM-yyyy"];
NSDate *date1 = [myDateFormat dateFromString:myString1];
[myDateFormat setDateFormat:#"MM-dd-yyyy"];
NSDate *date2 = [myDateFormat dateFromString:myString2];
[myDateFormat release];
If those are the only formats, just check something simple like if the fifth character is a dash then it's second, otherwise first.
Is there a way (an elegant way that is) to extract a date format from a string containing a date so that it may be converted into an NSDate via NSDateFormatter?
i.e
string = #"2011-1-10";
format = [extractor extractFormat:string];// format would = yyyy-dd-mm
[formatter setDateFormatter:format];
NSDate * date = [formatter dateFromString:string];
If that is the only information you have then it is impossible. For example, "10-10-10".
Since my date is well formatted now all there is left for me to do is to print milliseconds too.
I already tried setDateFormat yyyy/mm/dd hh:mm:ss:ms
That didn't work. Any advice?
Try to use 'SS' specifier (with number of S's equal to number of digits you want to get - 3 in your case), e.g.
[formatter setDateFormat:#"yyyy/MM/dd hh:mm:ss:SSS"];
Try this format:
"yyyy/MM/dd HH:mm:ss.SSS"
To be more precise this is not working for me:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-ddTHH:mm:ssZ"];
NSDate *prevday = [formatter dateFromString:#"2011-04-07T22:00:48Z"];
prevday is returning NIL.
You need to inserts ticks in the string:
#"YYYY'-'MM'-'dd'T'HH':'mm':'ss'Z'"
According to this document;
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html%23//apple_ref/doc/uid/TP40002369-SW1
Date formatters use the following version of the standard;
http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns
so you'd use the date format 'yyyy-MM-ddTHH:mm:ss' I'm not sure about the Z, is that supposed to be the time zone? Z in the format string will give you the 3 letter time zone name.
Edited based on your edit above:
[formatter setDateFormat:#"YYYY-MM-ddTHH:mm:ssZ"];
Is why it's returning nil.
The 'Z' expects the time zone to be in the 3 character time zone thing...
Drop the Z or escape it with a quote character. Read the tr35 doc above for more info.