date returned from dateFromString method - ios

Please help me solve the problem.
(The values for objValidation.aRange.MinimumValue, objValidation.aRange.MaximumValue come from web services)
(I am getting wrong dates in firstDate, secondDate.)
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy"];
[df setTimeZone:[NSTimeZone localTimeZone]];
NSDate *myDate = [df dateFromString:inputString];
[df setDateFormat:#"MM/dd/yyyy"];
NSLog(#"objValidation.aRange.MinimumValue : %#,objValidation.aRange.MaximumValue :%#, inputString : %#",objValidation.aRange.MinimumValue,objValidation.aRange.MaximumValue,inputString);
NSDate *firstDate = [df dateFromString:objValidation.aRange.MinimumValue];
NSDate *secondDate = [df dateFromString:objValidation.aRange.MaximumValue];
NSLog(#"myDate : %#,firstDate :%#, secondDate : %#",myDate,firstDate,secondDate);
[df release];
Output:
objValidation.aRange.MinimumValue : 8/20/2013
objValidation.aRange.MaximumValue :12/31/9999
inputString : 08/22/2013
myDate : 2013-08-21 18:30:00 +0000
firstDate :2013-08-19 18:30:00 +0000
secondDate : 1999-12-30 18:30:00 +0000

The date retuned is correct, but you do not take in account the timezone.
All NSDate object do not have an timezone. So when you parse the date the system timezone will be used.

Try This Code:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"MM/dd/yyyy"];
NSDate *date = [df dateFromString:#"8/20/2013"];
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date];
NSLog(#"DateString : %#", destinationDate);
You will get exact output which you want.

Related

what is the reason for one day delay in date value?

below is my sample code.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate * dateFormatted = [dateFormatter dateFromString:#"2016-01-17"];
NSLog(#"Date : %d",dateFormatted);
The output is 2016-01-16 18:30:00 +0000. How to get date Formatt 2016-01-17 . what is the reason for one day delay. please help me.
You shouldn't create the NSDate from the NSString without timezone mark.
Add timezone to the NSString like "-0800":
e.g. "2016-01-17 -0800" --> "yyyy-MM-dd Z"
Add code:
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];// change timezone u want
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate * dateFormatted = [dateFormatter dateFromString:#"2016-01-17"];
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:dateFormatted];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:dateFormatted];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:dateFormatted] ;
NSLog(#"Date : %#",destinationDate);
You can use below code...!
I am forcing the Date to be 2016-01-17 in first 2 scenarios. U can check and use if it is useful to you..!
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate * sourceDate = [dateFormatter dateFromString:#"2016-01-17"];
int daysToAdd = 1;
NSDate *presentDate = [sourceDate dateByAddingTimeInterval:60*60*24*daysToAdd]; //Output 2016-01-17 18:30:00 +0000
NSDate * dateFormatted = [dateFormatter dateFromString:#"2016-01-17"];
NSCalendar *calendar1 = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components1 = [calendar1 components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:dateFormatted];
[components1 setHour:24];//24
[components1 setMinute:00];//00
NSDate *presentDate1 = [calendar1 dateFromComponents:components1]; //Output 2016-01-17 18:30:00 +0000
//The input
NSString *myDateAsAStringValue = #"2016-01-17";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[df setDateFormat:#"yyyy-MM-dd"];
//Getting Date
NSDate *myDate = [df dateFromString: myDateAsAStringValue]; // Output 2016-01-17 00:00:00 +0000
//create the formatter for the output
NSDateFormatter *out_df = [[NSDateFormatter alloc] init];
[out_df setDateFormat:#"yyyy-MM-dd"];
NSString *dateStr=[out_df stringFromDate:myDate];//2016-01-17
Hope it helps you...!

iOS convert date to local time zone?

I know this is a very question and asked multiple times before.But this is something i get stuck every time.
Now, i want to convert a date string from web service which is in Argentina local zone(UTC-3:0) and want to convert this that to device local time zone(Suppose UTC+5:30). Here is my code
-(NSDate *)getLocaDateStringFromDate:(NSString *)sourceDate andSourceTime:(NSString *)sourceTime
{
//sourceDate is #"2015-10-16"; and sourceTime is #"00:00:00"
static NSDateFormatter* df = nil;
if (!df) {
df = [[NSDateFormatter alloc]init];
df.dateFormat = #"yyyy-MM-dd HH:mm:ss";
}
NSString* source = [sourceDate stringByAppendingString:[NSString stringWithFormat:#" %#", sourceTime]];
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:#"ART"];
[df setTimeZone: sourceZone];
NSDate *ds = [df dateFromString:source];
NSLog(#"sourceZone time is %#" , [df stringFromDate: ds]);//sourceZone time is 2015-10-16 00:00:00, correct!
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone: localTimeZone];
NSLog(#"local time is %#" , [df stringFromDate: ds]);//local time is 2015-10-16 08:30:00,, correct
NSString* str = [df stringFromDate: ds];
return [df dateFromString:str]; //this return 2015-10-16 03:00:00 +0000, why?
}
My question is that why my method return date in UTC, regardless of my time zone set to systemTimeZone. I get correct string but date is incorrect.
Any help would be appreciated.
1.) First, create an NSDateFormatter to get the NSDate sent from the server.
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
2.) Convert the date string to a NSDate.
NSDate* sourceDate = [dateFormatter dateFromString:dateString];
3) Specify ,Local and Destination timezone in which you want to convert your date.
For getting timezone of your source date:
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"GMT"];
For getting User timezone :
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
4.) Calculate the inverval between source timezone and user timezone as given bellow:
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
5.) Now, create an NSDateFormatter for formatting date in which you want to show.
NSDateFormatter* dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* localTime = [dateFormat stringFromDate:destinationDate];
NSLog(#"localTime:%#", localTime);
Hope it is useful for you :)

iOS NSDate get timeZone specific date?

I'm getting time in HH:mm:ss format from web service and it is in Argentina(GMT-3) time zone. I want to convert this time into full date (dd-MM-yyyy HH:mm:ss) and finally convert it into device local time zone's date.
Here is my code
-(NSString *)getLocalTimeStringFrom:(NSString *)sourceTime
{
static NSDateFormatter* df = nil;
static NSDateFormatter* df1 = nil;
if (!df) {
df = [[NSDateFormatter alloc]init];
df.dateFormat = #"dd-MM-yyyy HH:mm:ss";
}
if (!df1) {
df1 = [[NSDateFormatter alloc]init];
df1.dateFormat = #"dd-MM-yyyy";
}
NSTimeZone *sourceZone = [NSTimeZone timeZoneWithAbbreviation:#"ART"];
[df setTimeZone: sourceZone];
[df1 setTimeZone: sourceZone];
NSString *artDate = [df1 stringFromDate:[NSDate date]]; // get 29-09-2015
NSString* timeStamp = [artDate stringByAppendingString:[NSString stringWithFormat:#" %#",sourceTime]]; // get 29-09-2015 00:05:00, if sourceTime is 00:05:00
NSDate *art = [df dateFromString:timeStamp];
NSLog(#"ART date %#" , art);//ART date 2015-09-29 03:05:00 +0000
NSTimeZone *localTimeZone = [NSTimeZone systemTimeZone];
[df setTimeZone: localTimeZone];
NSLog(#"Local date %#" , [df stringFromDate: art]);
return [df stringFromDate: ds];
}
The problem is that i'm getting UTC date in art (as commented in code),please note that the value of art is 2015-09-29 03:05:00 +0000 which is in UTC and format is yyyy-MM-dd HH:mm:ss, but is should be in ART and dd-MM-yyyy HH:mm:ss. I tried some code from net but didn't get solution. What is wrong in this code?
I use this:
NSDateFormatter* serverDf = [[NSDateFormatter alloc] init];
[serverDf setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-3*60*60]];
[serverDf setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSDate* gmtDate = [serverDf dateFromString:timeStamp];
// DDLogVerbose(#"DateTime in GMT: %#", gmtDate);
NSDateFormatter* localDF = [[NSDateFormatter alloc] init];
[localDF setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
[localDF setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"en_US_POSIX"]];
NSString *localDate = [localDF stringFromDate:gmtDate];
May you just want a formate NSString , but not a NSDate,
Because I think NSDate will add a '+0000'
Try this.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:yourString];
NSString *str = [dateFormatter stringFromDate:date];

24 hour time from date string

I am trying to get just the time and that too in 24 hour format from following string :
7/2/2015 2:30:00 PM
This is what I have tried :
-(NSString*)returnDate:(NSString*)dateString
{
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"MM/dd/yyyy hh:mm:ss aaa"];
NSDate *date = [dateFormatter1 dateFromString:dateString];
dateString = [date descriptionWithLocale:[NSLocale systemLocale]];
NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:#"UTC"];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;
NSDate *destinationDate = [[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date];
NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:#"HH:mm a"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];
dateString = [dateFormatters stringFromDate: destinationDate];
return dateString;
}
Output : 4:30
This is returning correct time but in 12 hour format. I want the time in 24 hour format.
Desired output : 16:30
There is way to much code in the question, try this:
NSString *dateString = #"7/2/2015 4:30:00 PM";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"MM/dd/yyyy hh:mm:ss aaa"];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter setDateFormat:#"HH:mm"];
dateString = [dateFormatter stringFromDate: date];
NSLog(#"dateString: %#", dateString);
Output:
dateString: 16:30

Dateformatter gives wrong time on conversation [duplicate]

This question already has answers here:
NSDate Format outputting wrong date
(5 answers)
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 9 years ago.
I am trying to convert my date string to NSDate but its return correct date and wrong time.
This is my code :
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);
It returns this :
lastUpdatedate : 2013-12-20 06:40:40 +0000
- [NSDate description] (which is called when passing it to NSLog) always prints the date object in GMT timezone, not your local timezone. If you want an accurate string representation of the date, use a date formatter to create a correct string according to your timezone.
As #Leo Natan said, - [NSDate description] always gives date in GMT timezone. If you want to convert into local timezone then use following code.
NSString *dateStr = #"2013-12-20 12:10:40";
// Convert string to date object
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSLog(#"lastUpdatedate : %#",[self getLocalTime:lastUpdatedate]);
-(NSDate *) getLocalTime:(NSDate *)date {
NSTimeZone *tz = [NSTimeZone defaultTimeZone];
NSInteger seconds = [tz secondsFromGMTForDate: date];
return [NSDate dateWithTimeInterval: seconds sinceDate: date];
}
OUTPUT:
lastUpdatedate : 2013-12-20 12:10:40 +0000
NSString *dateStr = #"2013-12-20 12:10:40";
NSDateFormatter *dateFormatterTest = [[NSDateFormatter alloc] init];
[dateFormatterTest setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatterTest setLocale:[NSLocale currentLocale]];
NSDate *d = [dateFormatterTest dateFromString:dateStr];
Set NSLocale in your code, and you get perfect result.
You can try this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat: #"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *lastUpdatedate = [dateFormatter dateFromString:dateStr];
NSTimeInterval sourceGMTOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:lastUpdatedate];
lastUpdatedate = [lastUpdatedate dateByAddingTimeInterval:sourceGMTOffset];
NSLog(#"lastUpdatedate : %#",lastUpdatedate);

Resources