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

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...!

Related

i need to convert time to date format

time is string format like 10:27:43. but when i convert in to date format. it is going to change Sat Jan 1 10:27:43 2000.but i need only time like 10:27:43
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss ZZZ yyyy"];
NSLog(#"Date is %#", dateTime);
// Convert to new Date Format
[dateFormatter setDateFormat:#"HH:mm:ss"];
newDate = [dateFormatter stringFromDate:dateTime];
NSLog(#" string Date is %#", newDate);
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init] ;
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
//[dateFormatter1 setDateFormat:#"dd/mm/yyyy"];
NSDate *dateFromString = [dateFormatter1 dateFromString:newDate];
NSLog(#"date format date %#",dateFromString);
you need to convert dateFormat as per date is coming and after convert it as you want.
Try this below code..
NSString *dateTime = #"Thu Jan 25 10:38:11 2018";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE MMM dd HH:mm:ss yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:dateTime];
NSLog(#"Date is %#", dateTime);
// Convert to new Date Format
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSString *result = [dateFormatter stringFromDate:dateFromString];
NSLog(#"result %#",result);
Split the time in date components and use that components as a time.
Check below code.
NSString *aStrTime = #"10:27:43";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:aStrTime];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:date];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger sec = [components second];
NSLog(#"%d:%d:%d", hour, minute, sec);
Note: change aStrTime and dateFormatPattern according to your need.
You can try the following code:-
For e.g:- if you are getting the date in 10-02-2017 12:30:50 format
NSString *strDate = #"10-02-2017 12:30:50";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-mm-yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:strDate];
NSLog(#"The Date is: %#", date);
// By this code you will get the date in NSDate form. Now you can change the format to get the time
[dateFormat setDateFormat:#"HH:mm:ss"];
NSString *strFinalTime = [dateFormatter stringFromDate:strDate];
NSLog(#"strFinalTime: %#",strFinalTime);

How to convert UTC date string to local time

I currently get time as UTC and I want it to be convert into local time zone
NSDate * startDate;
Example strDate - 14/9/2017 7.28 Am
Required format - 14/9/2017 1.52 pm
I need help with objective c.
NSDate * startDate ; //contain utc date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy - h:mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
but this is not correct time but date is correct
First convert NSString to NSDate.
NSDateFormatter *DateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZoneEDT =[NSTimeZone timeZoneWithName:#"GMT"];
[DateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[DateFormatter setTimeZone:timeZoneEDT];
[DateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss.SSS"];
NSDate *CurrentDate = [DateFormatter dateFromString:DateStr];
Then convert GMT time to local time.
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithName:#"Asia/Kolkata"];
[dateFormatter setTimeZone:sourceTimeZone];
NSString *dateRepresentation = [dateFormatter stringFromDate:dateFromString];
#athira try this. it works for me.
NSDate *startDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/M/yyyy h.mm a"];
[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
NSString *timestamp = [dateFormatter stringFromDate:startDate];
NSLog(#"date = %#",timestamp); //date = 14/9/2017 4.34 PM
it will print current time with proper GMT+5:30 and i have used localTimeZone.

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];

Time difference between 2 dates results in wrong or unknown values

I am trying to find the time difference between 2 dates. but i am getting values as -0.0000, nan, or -357683564. below is my code to find time difference. am i doing any wrong in this calculations ?
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.timeStyle = NSDateFormatterNoStyle;
formatter.dateFormat = #"YYYY-MM-dd HH:MM:SS ±HHMM";//MM/dd/yyyy
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:usLocale];
NSDate *intialTime= [formatter dateFromString:[NSString stringWithFormat:#"%#",[dateArray objectAtIndex:i]]];//[formatter dateFromString:#"12/11/2005"];
NSDate *presentDate = [NSDate date];
NSTimeInterval timeInterval = [presentDate timeIntervalSinceDate:intialTime];
int seconds = (int)ceil(timeInterval);
int mins = seconds / 60;
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = timeInterval / secondsInAnHour;
NSLog(#"Time interval in Mins:%d -- %.4f -- %d",mins,timeInterval,hoursBetweenDates);
In Log :
Time interval in Mins:-35791394 -- nan -- -2147483648
intialTime - (null) and presentDate - 2014-05-09 10:30:15 +0000
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setLocale:[NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"]];
NSDate *dateToday = [NSDate date];
NSString *date = #"09-05-14"; // Put your initial date here...
[df setDateFormat:#"HH:mm:ss"];
NSString *todayString = [df stringFromDate:[NSDate date]];
NSString *todaysDateTime = [date stringByAppendingString:todayString];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *initialDate = [df dateFromString:todaysDateTime];
NSTimeInterval ti = [initialDate timeIntervalSinceDate:dateToday];
I fixed my Issue by using NSTimeZone. here was my problem.it was displaying date as their server date and my current date was not matching to timezone and not able to format it. Thanks for you all friends for helping me to find my mistake.
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:#"EST"];
[dateFormatter setTimeZone:gmt];

date returned from dateFromString method

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.

Resources