About JSON Date Conversion error - iphone

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.

Related

Getting date in specified format [duplicate]

This question already has answers here:
Passing a formatted NSDate
(3 answers)
Closed 8 years ago.
I am getting current date in format "2013-02-10 09:26:52 +0000" by using [NSDate date].
I want date in format like "Feb 10".
Can anyone suggest correct dateFormat?
Use NSDateFormatter class:
NSDateFormatter * myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:#"ddMMyy" options:0 locale:[NSLocale currentLocale]];
NSString * formattedDate = [myFormatter stringFromDate:[NSDate date]];
Here you have info to choose the output format for setDateFormat: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html
Hi set the date formatter like this
[dateFormatter setDateFormat:#"MMM d, yyyy hh:mm:ss"];
hope it will work
It is working in my case :
NSDateFormatter* dateFormatterBegin = [[NSDateFormatter alloc] init];
[dateFormatterBegin setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];
NSString *dateStringBegin = #"2013-02-10 09:26:52 +0000";
NSDate* datebegin = [dateFormatterBegin dateFromString:[dateStringBegin stringByReplacingOccurrencesOfString:#":" withString:#"" options:0 range:NSMakeRange(dateStringBegin.length-3, 1)]];
[dateFormatterBegin setDateFormat:#"MMM-dd"];
NSString *beginDate = [[NSString alloc] initWithFormat:#"%s%#","Start Date : ",[dateFormatterBegin stringFromDate:datebegin]];
NSLog(#"Your date is : %#",beginDate);
[dateFormatterBegin release];
[beginDate release];
NSString *string = #"2013-02-10 09:26:52 +0000"; // The date in a string
//Convert the string to date with the same format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"]; // Set format
NSDate *date = [dateFormat dateFromString:string];
//Create the new date format using NSDateFormatter
NSDateFormatter *dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:#"MMM dd"]; // Set format
//Convert the date using dateFormat1
NSDate *formatDate= [dateFormat1 dateFromString:date]; // date with MMM dd

NSDate and NSDateFormatting and unix timestamp

So I have the following code:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:self.firstUsed];
NSLog(#"FIRST USED %# %f", dateString, [[NSDate date] timeIntervalSinceDate:self.firstUsed]);
FIRST USED 06/15/2012 10:42PM 716.087895
The thing that confuse me is that when I put in 173.755023 it doesn't translate back to June 15th 2012 10:42. How is this possible? I think the correct number should be 1339714440
Try this :
NSDate* referenceDate = [NSDate dateWithTimeIntervalSince1970: 0];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:#"GMT"];
int offset = [timeZone secondsFromGMTForDate: referenceDate];
int unix_timestamp = [curdate timeIntervalSince1970];
int Timestamp = unix_timestamp - offset;
Please write down Following Code :
NSDateFormatter *dtFormatAppointment = [[NSDateFormatter alloc] init];
[dtFormatAppointment setDateFormat:#"MM/dd/yyyy hh:mm a"];
NSString *dateString = [dateFormat stringFromDate:self.firstUsed];
NSTimeInterval time = [dateString timeIntervalSince1970];
NSLog(#"FIRST USED %# %.f", dateString, time);
this code is working for you.

Not able to convert date from string

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

How to convert a date string like "2011-01-12T14:17:55.043Z" to a long like 1294841716?

I need to convert a date in this string format:
"2011-01-12T14:17:55.043Z"
to a number like 1294841716 (which is the number of seconds [not milliseconds] since Jan. 1st, 1970). Is there an easy way to do this parsing?
Update: Here is the code I've got so far:
NSString *dateString = #"2011-01-12T14:17:55.043Z";
NSDateFormatter *inFormat = [[NSDateFormatter alloc] init];
[inFormat setDateFormat:#"yyyy-MM-ddTHH:mm:ss.nnnZ"];
NSDate *parsed = [inFormat dateFromString:dateString];
long t = [parsed timeIntervalSinceReferenceDate];
But t comes back as 0 every time.
Use NSDateFormatter to get a NSDate then use - (NSTimeInterval)timeIntervalSince1970 to get the seconds since 1970.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:#"2011-01-12T14:17:55.043Z"];
NSLog(#"Date: %#", date);
NSLog(#"1970: %f", [date timeIntervalSince1970]);
NSLog(#"sDate: %#", [formatter stringFromDate:date]);
[formatter release];

How to get date from a string (iPhone)?

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?