calculate time interval of format hh.mm.ss in iOS - ios

I have date in format hh.mm.ss , now i want difference between two time of current format in hours. How can this be done.
NSString *time1 = #"12.43.42";
NSString *time2 = #"07.54.43";
I want time in hours.

please follow like this
NSDateFormatter *df = [NSDateFormatter new];
df.dateFormat = #"hh.mm.ss";
NSDate *date1 = [df dateFromString:#"12.43.42"];
NSDate *date2 = [df dateFromString:#"07.54.43"];
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int numberOfHours = secondsBetween / (60 * 60);

Related

Find different between hours

I have two hours and minutes :
I got this time from user. that what is your start time and end time.
1) 10:00 AM
2) 06:00 PM
Now i have to find total time user worked for. Means it fetch
total time: 8:00
How do i get it ?
I am ding this code in Objective C.
I think it will help you.
NSString *time1 = #"10.00 pm";
NSString *time2 = #"06.00 pm";
NSDate *date1;
NSDate *date2;
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh.mm a"];
date1 = [formatter dateFromString:time1];
date2 = [formatter dateFromString:time2];
[formatter release];
}
NSTimeInterval interval = [date1 timeIntervalSinceDate: date2]; //[date1 timeIntervalSince1970] - [date2 timeIntervalSince1970];
int hour = interval / 3600;
int minute = (int)interval % 3600 / 60;
NSLog(#"%# %dh %dm", interval<0?#"-":#"+", ABS(hour), ABS(minute));
Try like this!
let calendar = NSCalendar.current
let datecomponents = calendar.dateComponents([.hour], from: start as Date, to: someDateTime as Date)
let hours = datecomponents.hour!
print("hours: \(hours)")
Try this function
-(NSString *)startTime:(NSString *)startTime AndWithAndTime:(NSString *)endTime{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"hh:mm a"];
NSDate *date1 = [formatter dateFromString:startTime];
NSDate *date2 = [formatter dateFromString:endTime];
NSTimeInterval interval = [date2 timeIntervalSinceDate:date1];
int hours = (int)interval / 3600; // integer division to get the hours part
int minutes = (interval - (hours*3600)) / 60; // interval minus hours part (in seconds) divided by 60 yields minutes
NSString *timeDiff = [NSString stringWithFormat:#"%d:%02d", hours, minutes];
return timeDiff;
}
NSLog(#"%#",[self startTime:#"10:00 AM" AndWithAndTime:#"06:00 PM"]);

timeIntervalSinceDate not returning correct answer objective c

I'm trying to get timeIntervalSinceDate to send a log in secs of how much time has elapsed since a previous date but it isn't working. I set it for 1 minute in the future each time I test it but I not get logs that correspond like the latest log was this: 2015-09-22 16:30:04.469 Wakey Wakey[49785:7077539] Seconds nan 2015-09-22 16:30:04.470. This is the code:
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date ];
NSDate *currentTime = [NSDate date];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd:MM:YYYY:ss:mm:hh"];
NSDate *dateTimeSeconds = [[NSDate alloc] init];
dateTimeSeconds = [dateFormatter1 dateFromString:dateTimeString];
NSTimeInterval secs = [currentTime timeIntervalSinceDate:dateTimeSeconds];
NSLog(#"Seconds %g", secs);
What should I do to fix it?
Why bother with NSDateFormatter. Try
NSTimeInterval seconds = [[NSDate date] timeIntervalSinceDate:dateTimePicker.date];
NSLog(#"seconds %.f", seconds);
Also use %.f instead of %g on the NSLog
Try with this
NSDate* date1 = someDate;
NSDate* date2 = someOtherDate;
NSTimeInterval distanceBetweenDates = [date1 timeIntervalSinceDate:date2];
double secondsInAnHour = 3600;
NSInteger hoursBetweenDates = distanceBetweenDates / secondsInAnHour;
Please try with below date format
kTimeStampFormat #"dd-MM-yyyy'T'HH:mm:ss.SSS'Z'"
Use UTC format for consistency

Convert epoch milliseconds to NSDate

I want to convert milliseconds elapsed since Jan 1 1970 to NSDate without loosing milliseconds. Every solution says to divide milliseconds by 1000 and use dateWithTimeIntervalSince1970. but I want to keep the preserve the to milliseconds as well.
The parameter to dateWithTimeIntervalSince1970 is a NSTimeInterval, which is not an integer value (it's a double). There's no reason to lose milliseconds. Just don't use integers when you perform your division.
For example:
long long milliseconds = 1576058147753;
NSTimeInterval seconds = (NSTimeInterval)milliseconds / 1000.0;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:seconds];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale localeWithLocaleIdentifier:#"en_US_POSIX"];
[formatter setLocalizedDateFormatFromTemplate:#"dMMMMyyyyHHmmssSSS"];
December 11, 2019, 01:55:47.753
Note the milliseconds of 753 are there.
You can get date in this way.You have to pass timeinterval and date format as per your requirement.
-(NSString *)getStringFromDate:(double)interval withFormat:(NSString*)format
{
if (interval == 0)
{
return #"";
}
double seconds = interval;
NSTimeInterval timeInterval = (NSTimeInterval)seconds;
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter* df_utc = [[NSDateFormatter alloc] init];
[df_utc setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df_utc setDateFormat:format];
NSString *Date=[df_utc stringFromDate:date];
return Date;
}
Hope this will help you.

Convert date to timestamp in iOS

How do you convert any given date to milliseconds? For example, 2014-01-23 to timestamp conversion.
NSDate *date = [dateFormatter dateFromString:#"2014-01-23"];
NSLog(#"date=%#",date);
NSTimeInterval interval = [date timeIntervalSince1970];
NSLog(#"interval=%f",interval);
NSDate *methodStart = [NSDate dateWithTimeIntervalSince1970:interval];
[dateFormatter setDateFormat:#"yyyy/mm/dd "];
NSLog(#"result: %#", [dateFormatter stringFromDate:methodStart]);
Output result: 1970/30/01
Swift
Convert the current date/time to a timestamp:
// current date and time
let someDate = Date()
// time interval since 1970
let myTimeStamp = someDate.timeIntervalSince1970
See also
Convert Date to Integer in Swift
Creating a Date and Time in Swift
How to get the current time as datetime
Have it a try. "mm" stands for minute while "MM" stands for month.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init] ;
[dateFormatter setDateFormat:#"yyyy-MM-dd"] ;
NSDate *date = [dateFormatter dateFromString:#"2014-01-23"] ;
NSLog(#"date=%#",date) ;
NSTimeInterval interval = [date timeIntervalSince1970] ;
NSLog(#"interval=%f",interval) ;
NSDate *methodStart = [NSDate dateWithTimeIntervalSince1970:interval] ;
[dateFormatter setDateFormat:#"yyyy/MM/dd "] ;
NSLog(#"result: %#", [dateFormatter stringFromDate:methodStart]) ;
NSTimeinterval is really a double, being seconds since a particular date (in your case, the start of 1970)
NOTE :- UNIX Timestamp format contains 13 Digits so we need a 1000 multiplication with the result.
And the timestamp must be UTC ZONE not our local Time Zone.
- (void)GetCurrentTimeStamp
{
NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
[objDateformat setDateFormat:#"yyyy-MM-dd"];
NSString *strUTCTime = [self GetUTCDateTimeFromLocalTime:#"2014-01-23"];
NSDate *objUTCDate = [objDateformat dateFromString:strUTCTime];
long long milliseconds = (long long)([objUTCDate timeIntervalSince1970] * 1000.0);
NSLog(#"Local Time = %#---- UTC = %# ----- TimeSatmp = %ld ---- TimeStamp = %lld",strTime,strUTCTime,unixTime,milliseconds);
}
- (NSString *) GetUTCDateTimeFromLocalTime:(NSString *)IN_strLocalTime
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSDate *objDate = [dateFormatter dateFromString:IN_strLocalTime];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
NSString *strDateTime = [dateFormatter stringFromDate:objDate];
return strDateTime;
}
[date1 timeIntervalSinceDate:date2] will return seconds from two NSDate objects.
double seconds = [date1 timeIntervalSinceDate:date2];
double milliSecondsPartOfCurrentSecond = seconds - [seconds intValue];
milliSecondsPartOfCurrentSecond

JSON date string conversion issue in iOs

I am getting some UTC date strings from WCF Rest service and here is the format:
/Date(1354851639500+0530)/
I used the following code to convert the date:
//jsonDateString = 1354851639500+0530
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT]; //get number of seconds to add or subtract according to the client default time zone
NSTimeInterval unixTime = [[jsonDateString substringWithRange:NSMakeRange(0, 13)] doubleValue] / 1000; //WCF will send 13 digit-long value for the time interval since 1970 (millisecond precision) whereas iOS works with 10 digit-long values (second precision), hence the divide by 1000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss a ZZZZ"];
NSString *stringFromDAte = [dateFormatter stringFromDate:[[NSDate dateWithTimeIntervalSince1970:unixTime] dateByAddingTimeInterval:offset]];
NSLog(#"Server GMT: %#", stringFromDAte);
NSDate *currentDadte = [dateFormatter dateFromString:stringFromDAte];
NSTimeInterval interval = [currentDadte timeIntervalSinceDate:[NSDate date]];
return [self dailyLanguage:interval];
But when I convert the time is not correct. I need to get the UTC time of the receiving time. But I am getting the time value without the offset value.
For example: if josnDate = 1354851639500+0530,
i am getting, 2012-12-07 03:40:39 AM GMT, but i should get 2012-12-07 09:10:39 (approx).
How can I do this? Please help.
Try this Code
- (NSDate *)deserializeJsonDateString: (NSString *)jsonDateString
{
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT]; //get number of seconds to add or subtract according to the client default time zone
NSInteger startPosition = [jsonDateString rangeOfString:#"("].location + 1; //start of the date value
NSTimeInterval unixTime = [[jsonDateString substringWithRange:NSMakeRange(startPosition, 13)] doubleValue] / 1000; //WCF will send 13 digit-long value for the time interval since 1970 (millisecond precision) whereas iOS works with 10 digit-long values (second precision), hence the divide by 1000
[[NSDate dateWithTimeIntervalSince1970:unixTime] dateByAddingTimeInterval:offset];
return date;
}
For more detail answer follow this link
http://goo.gl/lE6ut
You can try this
NSString *jsonDateString = [SingleResponseObject objectForKey:#"datetime"];;
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval unixTime = [[jsonDateString substringWithRange:NSMakeRange(0, 13)] doubleValue] / 1000;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:#"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss a ZZZZ"];
NSString *stringFromDAte = [dateFormatter stringFromDate:[[NSDate dateWithTimeIntervalSince1970:unixTime] dateByAddingTimeInterval:offset]];
cell.date.text = stringFromDAte;

Resources