I am using below code to get date and time from datepicker.
When i am using formatter it is giving correct time .
but when i am converting that string into NSdate object ,then it is not giving the correct value of time.
NSDate *choice = [datepicker date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:#"MMM dd, yyyy HH:mm"];
NSString *dateString = [format stringFromDate:choice];
NSDate *date = [format dateFromString:dateString]; NSLog(#"%#",date);
DateString is giving correct value but when i am converting it in to NSdate again it is giving wrong value.
If the data that is being logged has some hour in difference then its ok since logging an NSDate will log it in GMT time zone, then this is not a bug, if your dateString is correct and your NSdate has some hours of difference then you have no issues
Related
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.
How to get my country time in UTC for iPhone development?
Here is the Code, Just Call below method when you want to set TimeZone and Date Format.
-(void)setDateFormat
{
NsDate myDate = [NSDate date];//here it returns current date of device.
//now set the timeZone and set the Date format to this date as you want.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSString *newDate = [dateFormatter stringFromDate:myDate];
// here you have new Date with desired format and TimeZone.
}
-(NSString *)UTCFormDate:(NSDate *)myDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:timeZone];
NSString *dateString = [dateFormatter stringFromDate:myDate];
return dateString;
}
Source:
https://stackoverflow.com/a/2615847/857865
[NSDate date] gives you the current point in time, which may be printed in UTC, GMT, EST or any other timezone. (Representation-wise, the current point in time is the number of seconds since a reference date in UTC.)
To get the current point in time in UTC as a string, get an NSDateFormatter and configure it to output a timestamp using the UTC timezone.
I want to set date in this format MMM dd, yyyy HH:mm, I hede done code for this but there is some thing wrong which I don't know.
NSDate *date = [NSDate date];
[format setDateFormat:#"MMM dd, yyyy HH:mm"];
NSLog(#"My date with out format = %#",date);
date = [format dateFromString:[format stringFromDate:date]];
NSLog(#"My date is = %#",date);
OUTPUT
My date with out format = 2012-06-22 09:53:46 +0000
My date is = 2012-06-22 09:53:00 +0000
Please help me where I am doing wrong.
Thanks in advance.
Firstoff, NSDate does not know any thing about the formatting of the date. Thats where NSDateFormatter comes in.
date = [format dateFromString:[format stringFromDate:date]];
What you are doing here is create a string for the given format, which will also result in removing the timezone. and than create a new date from that string. Since it no longer has the timezone it will result in a date with GMT as time zone.
But this will still result in a NSDate which holds, seconds and has a default time zone set to GMT.
There is no need to make a new date object, if you want to display a date just take the string which the stringFromDate: method returns. If you do it like this it will work:
NSDate *date = [NSDate date];
[format setDateFormat:#"MMM dd, yyyy HH:mm"];
NSLog(#"My date with out format = %#",date);
NSString *dateString = [format stringFromDate:date];
NSLog(#"My date is = %#",dateString);
You have not told the formatter to show the seconds, to do this add "HH:mm:ss"
NSDate *date = [NSDate date];
[format setDateFormat:#"MMM dd, yyyy HH:mm:ss"];
NSLog(#"My date with out format = %#",date);
date = [format dateFromString:[format stringFromDate:date]];
NSLog(#"My date is = %#",date);
Try this:
NSDateFormatter *format = [[NSDateFormatter alloc] init];
NSDate *date = [NSDate date];
[format setDateFormat:#"MMM dd, yyyy HH:mm:ss"];
NSLog(#"My date with out format = %#",date);
NSString *dateStr = [format stringFromDate:date];
//date = [format dateFromString:[format stringFromDate:date]];
NSLog(#"My date is = %#",dateStr);
[format setDateFormat:#"YYYY-dd-MM HH:mm:ss z"];
I am trying to convert the following string into an NSDate object:
NSString *str=#"25 May 2012 10:25:00";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"d MMM yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"asia/kolkata"]];
NSDate *date = [dateFormatter dateFromString:str];
In console : date-->2012-05-25 04:55:00 +0000....it lags behind 5 hours and 30 minutes and assumes GMT timezone instead of Asia...Why it is so?
When you see an [NSDate description] printed in the console, it is always the corresponding time in GMT. If you use the same date formatter to convert the date back to a string, it should be in the specified time zone.
An [NSDate description] is what you see if you type
po date
or
po [date description]
or you use NSLog to send either one of these forms to the console.
if you are looking for India Timezone you should use:
How can I compare the dates only, not the time. I am using
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSString *tempDate = #"2-2-2012"; //Dynamic Date
NSDate *dateString = [dateFormatter dateFromString:tempDate];
NSLog(#"%#",dateString);
It logs this: 2012-02-01 18:30:00 +0000
NSDate *now = [NSDate date];//Current Date
NSLog(#"%#",now);
It logs this: 2011-04-04 14:49:45 +0000
I want to compare Dynamic date and current date, I don't need time. I may not using the correct NSDateFormatter. Can anyone of you tell me how to do this? If I am not clear, please let me know.
Suppose I have to strings
date1 = 3-2-2011;
date2 = 4-5-2020;
I want to convert them in date, only after that I can compare them. Its not happening from my date Formatter. Please have a look.
Thanks!
Simplest way is to compare date by converting it into string.
Sample Code is as shown below:
//Current Date
NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd"];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];
//Other Date say date2 is of type NSDate again
NSString *date2String = [formatter stringFromDate:date2];
//Comparison of Two dates by its conversion into string as below
if([date2String isEqualToString:dateString])
{
//Your logic if dates are Equal
}
else if(![date2String isEqualToString:dateString])
{
//Your Logic if dates are Different
}
EDIT:
Checkout this link.
Comparing dates
http://www.iphonedevsdk.com/forum/iphone-sdk-development/64625-how-compare-2-dates.html
Hope This Helps You. :)
Use NSCalendar and NSDateComponents to get a date components object. Then you can look at only those parts of the date that you care about.
If you're just trying to determine whether two dates are the same, regardless of time, one way to go is to use NSDate's -timeIntervalSinceDate: method. If the time interval returned is less than 86,400 seconds (i.e. 24 hours * 60 minutes * 60 seconds) then you can feel fairly sure that it's the same day. Changes related to such things as daylight savings time and leap seconds introduce some possibility of error... if that's a problem, go with NSDateComponents.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = nil;
formatter=[[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterNoStyle];
[formatter setLocale:[NSLocale autoupdatingCurrentLocale]];
NSString *dateString = [formatter stringFromDate:date];
[formatter release];