I'm making an app where I need to get the actual date ([NSDate date]) into seconds since 1970 (timeIntervalSince1970). But I don't manage to get it working, how should it be done?
NSTimeInterval seconds = [[NSDate date] timeIntervalSince1970]
Note that NSTimeInterval is actually a double value, not an object.
double intrval=[[NSDate date] timeIntervalSince1970];
-(NSDate*)dateFromDouble:(double) intrval{
return [NSDate dateWithTimeIntervalSince1970:intrval];
}
Related
I'm getting a curious issue when creating NSDates using dateWithTimeIntervalSince1970 on a simulator versus an iPhone device. I am simply translating an ISO8601 timestamp from an NSDate to milliseconds and then back to a NSDate.
NSDateFormatter *iso8601Formatter = [[NSDateFormatter alloc] init];
iso8601Formatter.dateFormat = TSMISO8601FormatString; // "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
NSDate startDate = [iso8601Formatter dateFromString: #"2014-03-19T09:46:00-06:00"];
long startTimeMilliseconds = [startDate timeIntervalSince1970] * 1000;
startDate = [NSDate dateWithTimeIntervalSince1970:(startTimeMilliseconds / 1000)];
When I run this on the simulator, I get the same date back but when I run it on my iPhone, I get back this. I'm really confused as to why I'm getting back a wildly different day
1970-01-25 20:31:23 +0000 // iPhone device results
I have checked to make sure that my timezone, date, and time format are the same on both just to make sure that this isn't the issue. And both are running iOS 7.1. I have also attempted to set both the locale and timezone for the NSDateFormatter without any success.
Any help would be appreciated
This:
long startTimeMilliseconds = [startDate timeIntervalSince1970] * 1000;
Should Be:
double startTimeMilliseconds = [startDate timeIntervalSince1970] * 1000;
Here's why,
The maximum long can be found with LONG_MAX and it will log:
2147483647
Your current time interval is:
1395243960000.000000
Long can't go big enough.
I am trying to get current date with adding device time zone but is show 1 hr late that original date. I thing , I am getting problem of DaylightSavingTime.
How to disable isDaylightSavingTime = FALSE .
here is the code, I have used..
NSDate *date = [NSDate Date];
NSTimeZone *currentTimeZon = [NSTimeZone localTimeZone];
if ([currentTimeZon isDaylightSavingTime])
{
NSLog(#"East coast is NOT on DT");
}
else
{
NSLog(#"East coast is on DT");
}
NSTimeInterval timeZoneOffset = [currentTimeZon secondsFromGMTForDate:currentDate];
NSTimeInterval gmtTimeInterval = [date timeIntervalSinceReferenceDate] - timeZoneOffset;
NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
NSLog(#"### Announcement gmtDate = %#",gmtDate);
I am getting time with 1 Hour difference, date is perfact.
Use NSCalendar, it understands time zones, daylight savings, etc. As #albertamg says, NSDate is just the time since a reference at UTC (GMT), it has no other concept.
I want to present a date to the user of my app as "Today", "Yesterday" or as a formatted date (i.e. 27/05/2011). Is there a quick way to get "Today" or "Yesterday" based on a given NSDate? If not I can write the code myself, I am just curious if I am overlooking some simpler way than working out remaining hours manually.
If you just want to present date to your user, there is an option in NSDateFormatter right for that.
- (void)setDoesRelativeDateFormatting:(BOOL)b
Take a look at documentation for more information.
Check out this similar question: Compare NSDate for Today or Yesterday.
You make NSDate objects from today and yesterday, and then compare the first 10 characters of their description to the NSDate you're unsure of.
From the Date and Time Programming Guide:
NSTimeInterval secondsPerDay = 24 * 60 * 60;
NSDate *today = [[NSDate alloc] init];
NSDate *tomorrow, *yesterday;
tomorrow = [today dateByAddingTimeInterval: secondsPerDay];
yesterday = [today dateByAddingTimeInterval: -secondsPerDay];
[today release];
anyone know or can provide some example code relating to "timeIntervalSinceNow" method...
I need something like... time2(when app eneters foreground) - time1(when app enters background) = time3(the difference in times)... this is so i can use this number(pref in seconds) to calculate the time i have lost while the app has been in background !!
I am having trying trying to create the date objects, receive the object and display/use in a label....
Actually, to answer your original question, myles, you can use timeIntervalSinceNow.
In the statement below, inputDate has been initialized as an NSDate and set to some date (you could just try [NSDate *inputDate = [NSDate date]; to set the date at the current date and time.
NSTimeInterval timeToAlert =[inputDate timeIntervalSinceNow];
The next line is a way to put that NSTimeInterval into a string.
NSMutableString *timeinterval = [NSMutableString string];
[timeinterval appendFormat:#"%f",timeToAlert];
Finally, the app delegate class is typically where code can be written to handle coming in and out of background. Good luck!
timeIntervalSinceNow tells you the offset of an NSDate from the current time. You want timeIntervalSinceDate::
NSDate *appEnteredForeground = ...;
NSDate *appEnteredBackground = ...;
NSTimeInterval difference = [appEnteredBackground timeIntervalSinceDate: appEnteredForeground];
You can calculate the difference between two dates with the timeIntervalSinceDate: method:
//When app enters background:
self.backgroundDate = [NSDate date]; //this should be a property
//...
//When the app comes back to the foreground:
NSTimeInterval timeSpentInBackground = [[NSDate date] timeIntervalSinceDate:self.backgroundDate];
NSTimeInterval is simply a typedef for double, it's measured in seconds. [NSDate date] instantiates an NSDate object with the current date and time.
I initiated an NSDate with [NSDate date]; and I want to check whether or not it's been 5 hours since that NSDate variable. How would I go about doing that? What I have in my code is
requestTime = [[NSDate alloc] init];
requestTime = [NSDate date];
In a later method I want to check whether or not it's been 12 hours since requestTime. Please help! Thanks in advance.
NSInteger hours = [[[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:requestTime toDate:[NSDate date] options:0] hour];
if(hours >= 5)
// hooray!
int seconds = -(int)[requestTime timeIntervalSinceNow];
int hours = seconds/3600;
Basically here I'm asking how many seconds have passed since we first got our requestTime. Then with a little math magic, aka dividing by the number of seconds in an hour, we can get the number of hours that have passed.
A word of caution. Make sure you use the "retain" keyword when setting the requesttime. xcode likes to forget what NSDate objects are set to without it.
requestTime = [[NSDate date] retain];
Try using this method, or something along these lines.
- (int)hoursSinceDate :(NSDate *)date
{
#define NUBMER_OF_SECONDS_IN_ONE_HOUR 3600
NSDate *currentTime = [NSDate date];
double secondsSinceDate = [currentTime timeIntervalSinceDate:date];
return (int)secondsSinceDate / NUBMER_OF_SECONDS_IN_ONE_HOUR;
}
You can then do a simple check on the integer hour response.
int hours = [dateUtilityClass hoursSinceDate:dateInQuestion];
if(hours < 5){
# It has not yet been 5 hours.
}