How to convert NSString to NSDate using NSDateFormatter? - iphone

I getting the date from the webservice like "2012-07-03 07:26:48". I have saved in NSString after parsing from webservice. While convert the NSString to NSDate used below code,
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSString *dateStr = #"2012-07-03 07:26:48";
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(#"Date : %#", date);
The NSLog value is "2012-07-03 01:56:48 +0000";
The NSLog value is "2012-07-03 01:56:48 +0000"; I don't know why the time has changed. Can anyone please help me to do this?

You need to put timezone as:
theDateFormatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
Because it is considering GMT time format.

You have to set the time zone of the NSDateFormatter.

By default,Date is saved in utc format while converting string to nsdate... you can get original time from date as a string..if you want to change timezone, change it using dateformatter.. otherwise dont change

Related

Conversion from NSString to NSDate doesn't work

I have to convert this NSString: "12/13/1980" to a NSDate object.
I use a code like this:
NSString *birthday = #"12/13/1980"
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:dateString];
But the result is this: "1980-12-12 23:00:00 +0000"
Why? I'd like the same format and the same date.
Thank you very much.
The [dateFormat dateFromString:dateString] method produces an NSDate object which represents a single point in time.
When you NSLog a date object, it is printing a system representation of the NSDate object. I'm assuming your locale is GMT+1 .. so the NSLog prints 12/12/1980 23:00.
If you want to print the date object back, use your formatter to do [dateFormat stringFromDate:date];
It's because a NSDate object has always to have a time and a timezone, so if you don't specify that in your the string you're trying to convert IOS will use your local timezone and then guess the time, wich in this case will be 23:00 in UTC 0
The format is dependent on the localisation, there's also the option to set whether the date/time is short, medium or long format - I believe short is what you're looking for:
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html

NSDate and dateString in Objective-C

I want to save a date object to the backend of my App. This is the code:
NSDate *date = self.birthPickerView.date;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"YYYY-MM-dd HH:mm:ss"];
NSString *stringFromDate = [formatter stringFromDate:date];
NSLog(#"%#", stringFromDate);
NSDate *endDate = [formatter dateFromString:stringFromDate];
[formatter release];
NSLog(#"%#", endDate);
// Save to database
[user setObject:endDate forKey:#"birth"];
This is the print out result:
1985-03-05 00:00:00
1985-03-04 23:00:00 +0000
The end date is not right. I want to save 1985-03-05 in the database. Can you help me what is wrong?
Edit
[self.birthPickerView setTimeZone:[NSTimeZone localTimeZone]];
NSLog(#"%#", self.birthPickerView.date);
NSLog(#"%#", self.birthPickerView.timeZone);
[user setObject:self.birthPickerView.date forKey:self.navTitle];
This code save 1984-03-04 23:00:00 in database. what is wrong with it?
I don't see why would you need to convert your date into a string, and then back into the date again.
However, I'm pretty sure this happens because you didn't set the timezone on your NSDateFormatter. The default time zone is GMT which might cause the time offset you see there.

NSDate from NSString gives null result

I am using following code to generate NSDate -> NSString
+(NSString *)getCurrentTime
{
NSDate *now = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSString* str =[dateFormatter stringFromDate:now];
[dateFormatter release];
NSLog(#"%#",str);
return str;
}
everything is fine in above code. I am using above code to store string in Database. Now while retrieving that string gives me NULL. Following is my code to retrieve date in specific format
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:MM:SS a"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt =[dateFormatter dateFromString:crdInfo.swipeTime];
NSLog(#"Date : %#",dt);
[dateFormatter release];
How should I retrieve or store with particular format?? My crdInfo.swipeTime is retrieving String propertly...
First off, why not just store the NSDate object or epoch timestamp? This will give you much more flexibility in the future.
Now to your problem, I suspect it is due to your configuration of the NSDateFormatter, you're saving it in one format and trying to convert it to a date using a different format. Make the formats the same and try again. If you want to display it differently than it is stored you're likely going to need to convert it to and NSDate using the stored format and then again use another date formatter to get it in the format you want it as a string.
As Narayana suggested you need to retrieve the date with same format as you have stored. Retrieve it as below : -
NSDateFormatter *reDateFormatter = [[NSDateFormatter alloc] init];
[reDateFormatter setDateFormat:#"dd-MM-yyyy hh:MM:SS a"];
[reDateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dt = [reDateFormatter dateFromString:str];
NSLog(#"The Date : %#",dt);
[reDateFormatter setDateFormat:#"hh:MM:SS a"];
NSString *currentTime = [reDateFormatter stringFromDate:dt];
NSLog(#"%#",currentTime);
Hope it helps you.
Try to format it to dd-MM-yyyy hh:mm:ss a.
You wrote dd-MM-yyyy hh:MM:SS a where MM in hh:MM:SS gives month which is unrecognized in this format and there is no point writing upercase SS for seconds
Hope you understand it.

Getting NSDate from NSString

I am trying to convert NSString to NSdate. The string has date in it.i use the following
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:setitempricedate];
NSLog(#"Date : %#",date);
the input string contains the date in the format 02/08/2011
when i log date , i get 2011-02-07 18:30:00 GMT,
I want to get the date as 02/08/2011 only. Where am i going wrong
In your code, you are asking the date formatter to create a date object for you from a given string. Then you printed out that date object. What you want is to create that date object, then ask the date formatter to format that date object you just created. You should be calling stringFromDate instead.
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [dateFormat dateFromString:setitempricedate];
NSLog(#"Date: %#", [dateFormat stringFromDate:date]);
You will not be able to change the date representation. These (NSDateFormatter, NSCalendar) classes are provided to get formatted strings not date.
Whenever you will have an instance of NSDate class it will be in same format like you are getting.
2011-02-07 18:30:00 GMT
If you want custom styles better you go with NSString
You are printing the actual date object which doesn't follow the date format you specified. You could do something like
[[NSDate dateWithString:setitempricedate] stringFromDate];

is there any easy way to get Date?

I'm new to iPhone development. I want to set default date to NSDate Object as string. I don't see any easy way or method...
I think there might be a method in NSCalender? If there's such a method, please tell me.
Thanks in advance.
I'm not totally clear on what you are asking, but to create an instance of an NSDate object with the current date, one calls:
NSDate * myDate = [NSDate date];
If you are saying that you have a c-string or NSString that needs to be parsed to initialize an NSDate object, that's another question.
I have some code posted here:
How get a datetime column in SQLite with Objective C
that shows how to create NSDates from NSStrings using NSDateFormatter.
If you want to create an NSDate from a string, you need to use an NSDateFormatter to do it. It's important to note that the formatter will use the current locale's time zone when constructing the date, unless you put a time-zone in as part of the format. For more information about constructing time zones, see NSTimeZone.
For example, to create a date using the ubiquitous format '2011-01-16 00:00' in UTC, you would do:
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm"];
// Only certain abbreviations are okay, like UTC. See docs for more info
[formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSDate* midnight_26_jan_2011_utc = [formatter dateFromString:#"2011-01-26 00:00"];
// this will display in your system locale
// (for me, it shows 2011-01-25 19:00 +0500 because I'm America/New_York time)
NSLog(#"date: %#", midnight_26_jan_2011_utc);
[formatter release];
Edit: Added time to format string.
You will need to look at the NSDate and NSDateFormatter classes. Here's a simple example of how to use them:
NSString* defaultDateString = [NSString stringWithFormat:#"2011-01-22 15:30:00"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate* defaultDate = [dateFormatter dateFromString:defaultDateString];
[dateFormatter release];
and if you wanted to get the string from a date you can just use:
NSString* defaultDateString = [dateFormatter stringFromDate:defaultDate];
NSDateFormatter *DateFormatter=[[NSDateFormatter alloc] init];
[DateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
[DateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"]; //here,you can set the date format as you need
NSDate *now = [[[NSDate alloc] init]autorelease];
NSString *theDate = [DateFormatter stringFromDate:now];
Now, you can use the string the date. :)
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
[dateFormatter setDateFormat:#"yyyy-mm-dd"];
NSDate *yourDate = [dateFormatter dateFromString:#"2011-01-26"];