nsdate from string works only with date not time - iphone

im using a string to convert it to an nsDate
is working fine,
but when i try to add time to my string I get null on the logs
NSString *dateStringus = [NSString stringWithFormat:#"12-09-2011"];
NSLog(#"%#",dateStringus);
NSDateFormatter *dateFormattero = [[NSDateFormatter alloc] init];
[dateFormattero setDateFormat:#"dd-MM-yyyy"];
NSDate* d = [dateFormattero dateFromString:dateStringus];
NSLog(#"tomad tu string date ::%#",d);
NSString *pingus = [dateFormattero stringFromDate:d];
NSLog(#"transumatada ::%#", pingus);
NSLog(#"\n");
Works fine for only date,
but
if I do
NSString *dateStringus = [NSString stringWithFormat:#"12-09-2011 14:14:00"];
I get null on the logs
how to fix this string to include time? thanks!

The dateFormat property of the NSDateFormatter dateFormattero must match the format of date/time in the string dateStringus.
Change:
[dateFormattero setDateFormat:#"dd-MM-yyyy"];
Into:
[dateFormattero setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
Here is a good reference on date format patterns.

Related

iphone - How to convert rfc3399 formated date string to NSDate object?

I am getting the rfc3399 formatted date string through google calender web services. I want to convert this string into NSDate object so that I can add an event about this to the local calendar. I found the below method in Apple's documentation regarding NSDateFormatter , but its not working
Date String - 2012-05-23T18:30:00.000-05:00
- (NSString *)userVisibleDateTimeStringForRFC3339DateTimeString:(NSString *)rfc3339DateTimeString {
/*
Returns a user-visible date time string that corresponds to the specified
RFC 3339 date time string. Note that this does not handle all possible
RFC 3339 date time strings, just one of the most common styles.
*/
NSDateFormatter *rfc3339DateFormatter = [[NSDateFormatter alloc] init];
NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"];
[rfc3339DateFormatter setLocale:enUSPOSIXLocale];
[rfc3339DateFormatter setDateFormat:#"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
[rfc3339DateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
// Convert the RFC 3339 date time string to an NSDate.
NSDate *date = [rfc3339DateFormatter dateFromString:rfc3339DateTimeString];
NSString *userVisibleDateTimeString;
if (date != nil) {
// Convert the date object to a user-visible date string.
NSDateFormatter *userVisibleDateFormatter = [[NSDateFormatter alloc] init];
assert(userVisibleDateFormatter != nil);
[userVisibleDateFormatter setDateStyle:NSDateFormatterShortStyle];
[userVisibleDateFormatter setTimeStyle:NSDateFormatterShortStyle];
userVisibleDateTimeString = [userVisibleDateFormatter stringFromDate:date];
}
return userVisibleDateTimeString;
}
Please help me , I have tried changing the date format quite a lot times but no success.
try with this method..
-(NSDate *)convertStringToDate:(NSString *) date {
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
NSDate *nowDate = [[[NSDate alloc] init] autorelease];
[formatter setDateFormat:#"YYYY-MM-DD'T'HH:mm:ssZ"];// set format here which format in string date
/// also try this format #"yyyy-MM-dd'T'HH:mm:ss.fffK"
date = [date stringByReplacingOccurrencesOfString:#"+0000" withString:#""];
nowDate = [formatter dateFromString:date];
// NSLog(#"date============================>>>>>>>>>>>>>>> : %#", nowDate);
return nowDate;
}
call this method like bellow..
NSDate *date = [self convertStringToDate:rfc3339DateTimeString];
also for try with some rfc Date formate with fffK for this format see this bellow. link..
how-do-i-parse-and-convert-datetimes-to-the-rfc-3339-date-time-format
i hope this answer is helpful
The issue is the date contains (-05:00 )':' in the timezone value ... you have to remove it from the string then the above method will work fine......The new date string should look like below
2012-05-23T18:30:00.000-0500 - This should be the right format .

Convert integer to a date string

In Objective-C: Is there a simple way, as in Excel and SAS, to convert an integer into a datetime string?
In Excel, you'd format to "mmm dd, yyyy hh:mm:ss".
For instance, if I had the integer:
1328062560
and I want the output to be:
2012-01-31 21:16:00
or even (as in Excel):
Jan 31, 2012 21:16:00
PS: I don't want to be too demanding, but I'd like it simple and inline for use in NSLog, so I can write simply for debugging
NSLog("The time as an integer is %d and as a date %mmmddyyyyhh:mm:ss", [array objectAtIndex: i], [array objectAtIndex: i]);
You can get an NSDate object like this:
NSDate *date = [NSDate dateWithTimeIntervalSince1970:1328062560];
Then, if you don't care about the exact format of the string, you can just do this:
NSString *s = [date description];
That will give you a string like “2012-02-01 02:16:00 +0000”.
If you want a specific string format, use an NSDateFormatter.
You can use NSDateFormatter to do this. You first make a string from your integer value:
NSString *dateString = [NSString stringWithFormat:#"%d", integerDate];
And then create an NSDateFormatter with the corresponding format:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[myDateFormatter setDateFormat:#"yyyyMMddhhmm"]; // Change to suit your format
NSDate *dateFromString = [myDateFormatter dateFromString:dateString];
You can then retrieve the date as a string in the format you want:
[myDateFormatter setDateFormat:#"yyyy/MM/dd hh:mm"]; // Change to suit your format
NSString *stringFromDate = [formatter stringFromDate:dateFromString];
Assuming that integer is a time in seconds since the epoch, you can use NSDate's initWithTimeIntervalSince1970 or dateWithTimeIntervalSince1970 methods to create an NSDate object with the date you care about, and then use NSDateFormatter to pretty it up for display.

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

iphone NSDate Problem

Following code I can use for storing the value of UILabel into a string. And Value of string is store into NSDate.
NSString *star = [[NSString alloc]init];
star = lbtInDate.text;
NSString *end = [[NSString alloc]init];
end = lblOutDate.text;
NSDateFormatter *dateFormatter1=[[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm"];
NSDate *sdate= [dateFormatter1 dateFromString:star];
NSDate *edate=[dateFormatter1 dateFromString:end];
but the value of sdate and edate is shows nil. so please help how can i store the value of UILabel into NSDate for compare.
Your code is perfectly valid only the lbtInDate.text; and lblOutDate.text; respect the date format. If not your NSDate's will be null.
If lblOutDate.text; is something like #"2002-12-23", your date format should be #"yyyy-MM-dd" and so on.
Data Formatting Guide

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?