I am getting a date string back from a web service which looks like this:
/Date(12924576000000+0000)/
Can anyone tell me how to format this into a readable string?
Thanks.
Here is how I'm doing it:
NSString * json_parsed_date = #"/Date(12924576000000+0000)/";
NSString* tsString = [json_parsed_date substringWithRange:NSMakeRange(6,10)];
NSNumber* timestamp = [NSNumber numberWithInteger:[tsString intValue]];
NSDate * dateObject = [NSDate dateWithTimeIntervalSince1970:[timestamp intValue]];
For human readable string:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd"];
NSString *theDate = [dateFormat stringFromDate:dateObject];
Related
I get JSON Parse Date, I want to use NSDateFormatter,but not success.
1.JSON Get console Log
IncidentReceiveTime = "/Date(1353914100000+0800)/";
2.my code
NSString *dateStr =[NSString stringWithFormat:#"%#",[[[DateSortArry objectAtIndex:0] objectForKey:#"IncidentReceiveTime"]stringByReplacingOccurrencesOfString:#"/" withString:#""]];
NSLog(#"dateStr:%#",dateStr);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:#"yyyy-MM-ddHH"];
NSDate *date = [dateFormatter dateFromString:dateStr];
NSLog(#"date:%#",date);
3.NSDateFormatter console log
dateStr:Date(1361943694000+0800)
date:(null)
Refer to my answer in :
iPhone:How to convert Regular Expression into Date Format?
Here I am posting this code from my answer in the above link as if in case in future the above link goes down then this answer doesn't become useles:
- (NSDate*) getDateFromJSON:(NSString *)dateString
{
// Expect date in this format "/Date(1268123281843)/"
int startPos = [dateString rangeOfString:#"("].location+1;
int endPos = [dateString rangeOfString:#")"].location;
NSRange range = NSMakeRange(startPos,endPos-startPos);
unsigned long long milliseconds = [[dateString substringWithRange:range] longLongValue];
NSLog(#"%llu",milliseconds);
NSTimeInterval interval = milliseconds/1000;
return [NSDate dateWithTimeIntervalSince1970:interval];
}
Once you get NSDate object from above method:
NSDateFormatter *dateForm = [[NSDateFormatter alloc] init];
[dateForm setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString *dateStr = [dateForm stringFromDate:<YourNSDateObject>];
[dateForm setDateFormat:#"<Your Desired Date Format>"];
NSDate *desireddate = [dateForm dateFromString:dateStr];
Hope this helps.
I am doing an application where I am using the following link http://iosdevelopertips.com/cocoa/date-formatters-examples-take-3.html to convert the date from string.
I am using below code to same by following above tutorial link. when I NSlog PresentDateTime it gives 20120111 13:03:49.263+05:30.
so I am taking this format and want to convert into the format of "EEEE MMMM d, YYYY h:mm a, zzz".
To do so when I assign the same to the variable
NSString *today = presentDateTime;
when I NSLog Datestring I am getting result NULL can I know where I am going wrong? please help me to solve this issue by suggesting what changes to be done
//code
-(NSString *)DateFormat:(NSString *) presentDateTime
{
presentDateTime = [presentDateTime stringByReplacingOccurrencesOfString:#"T" withString:#" "];
presentDateTime = [presentDateTime stringByReplacingOccurrencesOfString:#" +" withString:#"+"];
presentDateTime = [presentDateTime stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSLog(#"Present date::::%#",presentDateTime);
NSString *today = presentDateTime;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyyMMdd HH:mm:ss:SSSZZZ"];
NSDate *date = [dateFormat dateFromString:today];
[dateFormat setDateFormat:#"EEEE MMMM d, YYYY h:mm a, zzz"];
NSString *dateString = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"dateString:::Date>>>>:%#::::\n%#",dateString, presentDateTime);
return dateString;
Your date format pattern for parsing the date is wrong/incomplete. For the string 20120111 13:03:49.263+05:30 the format would be YYYYMMdd HH:mm:ss.SSSZZZ. See this table for the format string patterns.
Edit: After playing around with the code, it turns out that the NSDateParser chokes on the timezone +05:30. It expects +0530, without the colon. Here's how to remove it:
NSString *today = #"20120111 13:03:49.263+05:30";
NSRange colon = [today rangeOfString:#":" options:NSBackwardsSearch];
if (colon.location != NSNotFound) {
today = [today stringByReplacingCharactersInRange:colon withString:#""];
}
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYYMMdd HH:mm:ss.SSSZZZ"];
NSDate *date = [dateFormat dateFromString:today];
NSLog(#"%#", date);
When you parse your original date string you need to specify the entire format, like this:
[dateFormat setDateFormat:#"yyyyMMdd HH:mm:ss.SSSZZZ"]
Compare your code with the following
NSString *dateString = #"6:30 18 Oct";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm dd/MMM"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
Its just sample to follow. and figure out what is going wrong by printing it in nslog.
Your date variable is always nil because your date format (yyyyMMdd) is incomplete as your input string today contains extra information (20120111 13:03:49.263+05:30). So you can just crop unnecessary characters:
if ([presentDateTime count] < 8) return nil;
NSString *today = [presentDateTime substringToIndex:8];
Or you should specify full date format as mentioned by #jonkroll
my question is simple, i want to load a UIImage from an URL, but this URL change programmatically by date.
Example Today the url is http://www.abc.com/2011-10-13/alfa.jpg
tomorrow is http://www.abc.com/2011-10-14/alfa.jpg
the only thing that change is the date part, how can i figure my app load that "alfa.jpg" at current date everytime i start it?
Thanks!
You should use [NSDate date] which returns the current date and time according to the device time (assuming your device time doesn't differ from the actual by more than a day). Then you should format the date according to your url. Something like-
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString* dateString = [formatter stringFromDate:[NSDate date]];
[formatter release];
Use an NSDateFormatter to generate the part of the string that varies with time.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString * datePart = [dateFormatter stringFromDate:[NSDate date]];
NSString * theURLString = [NSString stringWithFormat:#"http://www.abc.com/%#/alfa.jpg", datePart];
You will have to maintain a mechanism to check whether if the image for the day has been downloaded to avoid downloading it again.
Create a template of the url you are using nstring and a DateFormatter object...
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:#"yyyy-MM-dd"];
NSString *todayStr = [df stringFromDate:[NSDate date]];
NSString *todayUrl = [NSString stringWithFormat:#"http://www.abc.com/%#/alfa.jpg", todayStr];
todayUrl has the url for today!
Its an easy task just format your URL string using current date like,
NSString *URLString = [[NSString alloc] initWithFormat:#"http://www.abc.com/%#/alfa.jpg", currentDate];
Then use this string for URL request.
I have XML with this value:
<LastModifiedOnDate>2011-04-02T00:00:00</LastModifiedOnDate>
I am trying to parse this on iPhone from NSString to NSDate but have no luck.
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
//NSString *strConcat = #"2010-09-24 17:30:00";
NSDate *date = [formate dateFromString:string];
You don't need all those apostrophes in your format string. The only ones you need are around the T.
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
// I recommend setting a timezone because it will use the system timezone and that
// can cause confusion with your data.
// [formate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString *strConcat = #"2010-09-24T17:30:00";
NSDate *date = [formate dateFromString:strConcat];
NSLog(#"Date: %#", date); // remember NSDate default to current timezone when logging to console
If you string have fraction of seconds, then add SSS to your string format: [formate setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS"];
For additional information on date formatting check out Unicode Technical Standard #35.
Replace your's SetDataFormat function with below and let me know for the result ...
[formate setDateFormat:#"yyyy-MM-ddTHH:mm:ss"];
NSString *string=#"2011-04-05T12:43:56.537";
NSDateFormatter *formate = [[[NSDateFormatter alloc] init] autorelease];
[formate setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSS"];
NSDate *date = [formate dateFromString:string];
its working..
I need to convert this string in to the date:
02-09-2011 20:54:18
I am trying the `dateFromString` method but every time it is returning (null), what NSDateFormatter should I use?, I have tried
`[formatter setDateFormat:#"yyyy-MM-dd"]` and `[formatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]`.
Here is my code:
NSString *finalDate = #"02-09-2011 20:54:18";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSDate *dateString = [formatter dateFromString:finalDate];
NSLog(#"%#",dateString);
Thanks.
You can convert a NSString containing a date to a NSDate using the NSDateFormatter with the following code:
// Your date as a string
NSString *finalDate = #"02-09-2011 20:54:18";
// Prepare an NSDateFormatter to convert to and from the string representation
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// ...using a date format corresponding to your date
[dateFormatter setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
// Parse the string representation of the date
NSDate *date = [dateFormatter dateFromString:finalDate];
// Write the date back out using the same format
NSLog(#"Month %#",[dateFormatter stringFromDate:date]);
Surely it's easier to search than it is to ask?
How do I convert from NSString to NSDate?