Add 20 minutes to UTC date - ios

I have date in string format,that is with UTC time zone.
I want to add 20 minutes to date.
I am using following code for that:
NSString *strFinal = #"2016-11-21 13:07:04";
//NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//[dateFormatter setDateFormat:#"yyyy-mm-dd hh:mm:ss"];
self.dateFormatter = [[NSDateFormatter alloc]init];
[self.dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
[self.dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
dateFromString = [self.dateFormatter dateFromString:strFinal];
self.dt = [[NSDate alloc]init];
self.dt = dateFromString;
NSTimeInterval secondsInEightHours = 20 * 60;
//NSDate *dateEightHoursAhead = [dateFromString dateByAddingTimeInterval:secondsInEightHours];
NSDate *dateEightHoursAhead = [self.dt dateByAddingTimeInterval:secondsInEightHours];
//after adding 20 mins check
NSString *strFinaltoPass = [self.dateFormatter stringFromDate:dateEightHoursAhead];
but with this date time it gives strFinaltoPass as null.
This works fine with time less than 12 hours but after 12 hour its showing null.
Any help will be appreciated.
Thanks,

You can use NSCalendar to add minutes, here is an example
NSString *strDate = #"2016-11-21 13:07:04";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *dateTemp = [dateFormatter1 dateFromString:strDate];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *dateNew = [calendar dateByAddingUnit:NSCalendarUnitMinute value:20 toDate:dateTemp options:NSCalendarWrapComponents];
here value = 20 is 20 minutes to add in the date

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 subtract time from NSDate with the help of NSDateFormatter?

I'm working on an application in which user set a time to do a task and before 10 or 15 minutes application triggers an alarm. Just like a reminder app you can say.
After searching a lot on stackOverFlow and other different links my problem still exists. Here is what I'm doing:
-(void)timeWasSelected:(NSDate *)selectedTime element:(id)element {
if((UITextField *)element == self.timeForTask){
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
self.textField.text = [dateFormatter stringFromDate:selectedTime];
[defaults setObject:self.textField.text forKey:#"taskTime"];
myTime = [selectedTime dateByAddingTimeInterval:-600];
NSLog(#"New Time %#",myTime);
}
After doing this the NSLog part gives this output: New Time 2015-04-27 06:14:14 +0000
The problem is that I've selected time 11:22AM and it is storing 06:14 and I've only deducted 10min from the selected time.
Try this you will get output:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
NSDate* selectedTime = [dateFormatter dateFromString:#"11:22 AM"];
NSDate* myTime = [selectedTime dateByAddingTimeInterval:-60*10];//10 minute deduction
NSString* updateTime = [dateFormatter stringFromDate:myTime];
NSLog(#"New Time %#",updateTime);
Output :
New Time 11:12 AM
Here you are subtracting 10 Minutes from selected time. if you want 15 Minutes then replace with 10 digits.
Hope this help you.
Try this, may help you,
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
NSLog(#"1New Time %#",[dateFormatter stringFromDate:selectedTime]);
self.textField.text = [dateFormatter stringFromDate:selectedTime];
[defaults setObject:self.textField.text forKey:#"taskTime"];
NSTimeZone *destinationTimeZone = [NSTimeZone systemTimeZone];
[dateFormatter setTimeZone:destinationTimeZone];
NSDate *myTime = [selectedTime dateByAddingTimeInterval:-600];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setTimeZone:destinationTimeZone];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd hh:mm:ss ZZ"];
NSLog(#"2New Time %#",[dateFormatter1 stringFromDate:myTime]);
Enjoy Coding !!
// gives new date object with time 15 minutes earlier
NSDate *newDate = [oldDate dateByAddingTimeInterval:-60*15];
NSString* NewTime = [dateFormatter stringFromDate:newDate];
NSLog(#"New Time %#",newDate);
This code will exactly get you 15 minutes earlier time

Convert string into date in ios

I have my data in JSON format which contains some date:
"NOME_PUBBL_LINEA" = "FOLLONICA - PIOMBINO";
"ORA_ARRIVA" = "1899-12-30T06:45:00";
"ORA_PARTE" = "1899-12-30T05:50:00";
I want to convert it like :
ora_arriva = 6.45,
ora_parte = 5.50
I have tried following :
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-dd'T'HH:mm:ss'+02:00'"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:#"Italy/Rome"]];
NSDate *dte = [dateFormat dateFromString:dataDic[#"ORA_ARRIVA"]];
But it is giving me like
1899-12-30 00:51:40 +0000
for ora_arriva.
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'+02:00'"];
Just yyyy :)
dateFromString returns an NSDate. You need take that NSDate object and construct your time:
NSDate *myDate = [NSDate date];
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comp = [gregorianCalendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:myDate];
NSLog(#"current time = %ld:%ld", [comp hour], [comp minute]);
Replace the myDate with your actual NSDate you got back from your dateFormatter
You need something like this
[dateformatter setDateFormat:#"MM-dd-yyyy hh:mm:ss"];
NSString *dateString = [dateformatter stringFromDate:adate];

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

Trying to use NSDate for time only

I'm struggling to find a way to use NSDate for time only purposes.
I was trying to do the following code:
- (void)awakeFromInsert
{
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.minute = 45;
comps.second = 0;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
self.periodDuration = [calendar dateFromComponents:comps];
NSLog(#"periodDuration: %#",self.periodDuration);
comps = [[NSDateComponents alloc] init];
comps.hour = 8;
comps.minute = 0;
self.firstPeriodStartTime = [calendar dateFromComponents:comps];
NSLog(#"firstPeriodTime: %#",self.periodDuration);
}
But the result I get is:
periodDuration: 0001-12-31 22:24:04 +0000
firstPeriodTime: 0001-12-31 22:24:04 +0000
The result I was expecting:
periodDuration: 45:00
firstPeriodTime: 08:00
What am I doing wrong? How can I fix this?
Thank you.
The log of date is misleading you, if you are forming a date with only a few components you will not get a proper date instance which uniquely identifies a date and time.
If you try with this code snipped you can find that if you are just converting the dates back to string it is just as you expect
NSDateComponents *comps = [[NSDateComponents alloc] init];
comps.minute = 45;
comps.second = 0;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *periodDate = [calendar dateFromComponents:comps];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"mm:ss"];
NSLog(#"periodDuration: %#",[dateFormatter stringFromDate:periodDate]);
comps = [[NSDateComponents alloc] init];
comps.hour = 8;
comps.minute = 0;
NSDate *firstPeriodDate = [calendar dateFromComponents:comps];
[dateFormatter setDateFormat:#"HH:mm"];
NSLog(#"firstPeriodTime: %#",[dateFormatter stringFromDate:firstPeriodDate]);
periodDuration: 45:00
firstPeriodTime: 08:00
You can use NSDateFormatter
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"HH:mm"];
NSLog(#"%#",[dateFormatter stringFromDate:[NSDate date]]);
Use a NSTimeInterval (which is, basically, a double containing the number of seconds). To store it in CoreData:
[NSNumber numberWithDouble:myTimeInterval];
As for formatting: If you want more than 24h displayed, use this:
NSUInteger seconds = (NSUInteger)round(myTimeInterval);
NSString *formattedDate = [NSString stringWithFormat:#"%02u:%02u:%02u",
seconds / 3600, (seconds / 60) % 60, seconds % 60];
NSLog(#"%#", formattedDate);
If you don't want more than 24h, you can use NSDate and NSDateFormatter again:
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:myTimeInterval];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
[dateFormatter setDateFormat:#"HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(#"%#", formattedDate);
NSString *timeString;
timeString = [NSDateFormatter localizedStringFromDate:[NSDate date]
dateStyle:NSDateFormatterNoStyle
timeStyle:NSDateFormatterMediumStyle];
12:03:54 PM

Resources