I'm reading data from a XML file which has a UTC date looking like "2011-05-04T00:00:00", and a UTC epoch looking like 1352716800.
Parsing the UTC epoch to NSDate would probably be much safer than messing around with a complex date format. How would I parse the UTC epoch to NSDate? With NSDateFormatter and a special format for "UTC Epoch"?
I think that it is [[NSDate alloc] initWithTimeIntervalSince1970:epoch] and a test seemed to work. But I am not sure if that's just correct by accident or if the "UTC epoch" is "Since 1970". The Apple Docs don't mention UTC Epoch.
YES, you are correct it is UTC Epoch. For Reference if "Epoch time is UTC" checkout this
NSString *epochTime = #"1352716800";
// (Step 1) Convert epoch time to SECONDS since 1970
NSTimeInterval seconds = [epochTime doubleValue];
NSLog (#"Epoch time %# equates to %qi seconds since 1970", epochTime, (long long) seconds);
// (Step 2) Create NSDate object
NSDate *epochNSDate = [[NSDate alloc] initWithTimeIntervalSince1970:seconds];
NSLog (#"Epoch time %# equates to UTC %#", epochTime, epochNSDate);
You don't really need to parse the UTC epoch date. Instead you can more or less directly create an NSDate instance from it:
long utcEpoch = 1352716800;
NSDate* date = [Date dateWithTimeIntervalSince1970: utcEpoch];
In the case of the timestamp retrieved from Firebase (kFirebaseServerValueTimestamp), the epoch is expressed in milliseconds:
A placeholder value for auto-populating the current timestamp (time since the Unix epoch, in milliseconds) by the Firebase servers.
In that case dividing by 1000 is needed if you use initWithTimeIntervalSince1970 in iOS.
Related
i'm using xCode 7.2.1 ad Swift2, writing fo iOS.
i did a function for converting a String to a NSDate and that's :
class func StringToDateWithHour(data : String) -> NSDate
{
let dateFormatter = NSDateFormatter()
dateFormatter.timeZone = NSTimeZone(abbreviation: "CET");
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
guard let date = dateFormatter.dateFromString(data) else{
return NSDate()
}
return date
}
I don't understand why i get back the UTC time, INSTEAD of CET time.
When i try to convert a string like : "2016-2-21 10:00:00" i get back
"2016-2-21 09:00:00" , one hour less.
Thank you.
You're saying that you want to convert 10 am CET to a NSDate.
That correctly outputs 9 am UTC time (UTC is one hour behind CET time).
NSDate objects don't depend on time zones, they're just data from a particular point in time.
NSDate objects encapsulate a single point in time, independent of any particular calendrical system or time zone. Date objects are immutable, representing an invariant time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001).
How to find one text field value is within past 60 day excluding current date.
For example if I enter value in text field is 20-July-2012 using Date Picker.Then I click submit,it'll check that specific is date is within 60 days or not. If the values are entered which is before 60 days an alert message is displayed. The values are retrieved from api.
NSDate *today = [NSDate date];
NSTimeInterval dateTime;
if ([pickerDate isEqualToDate:today]) //pickerDate is a NSDate
{
NSLog (#"Dates are equal");
}
dateTime = ([pickerDate timeIntervalSinceDate:today] / 86400);
if(dateTime < 0) //Check if visit date is a past date, dateTime returns - val
{
NSLog (#"Past Date");
}
else
{
NSLog (#"Future Date");
}
Change the value of 86400 to suit your query.In this case, it is the number of seconds we want to compare.
First, convert the text into an NSDate. Then use
timeIntervalSinceDate:[NSDate dateWithTimeIntervalSinceNow:0]
There are a couple of ways to convert text into an NSDate. You can format the text correctly and then use dateWithString or you can convert everything into numbers, multiply them out, and one of the dateWithTimeInterval methods.
If you want the user to be able to enter "July" (plain text month) then you might want to write a method that converts months into their numerical equivalents with string matching.
NSDate *lastDate; //your date I hope you have created it
NSDate *todaysDate = [NSDate date];
NSTimeInterval lastDiff = [lastDate timeIntervalSinceNow];
NSTimeInterval todaysDiff = [todaysDate timeIntervalSinceNow];
NSTimeInterval dateDiff = lastDiff - todaysDiff; // number of seconds
int days = dateDiff/(60*60*24); // 5.8 would become 5 as I'm taking int
How do you define 60 days?
You may want to use NSCalendar -dateByAddingComponents:toDate:options: to ensure your 60 days really are 60 days.
NSCalendar also provides -components:fromDate: and -dateFromComponents: which are very nice when dealing with date components.
If 60 days do not need to be true calendar days (daylight saving time switches, astronomical time corrections, stuff like that), you can just have fun with NSDate and the time interval methods alone.
If I have set dates like Sunday Jan.29, 2012 2:00:00 PM and Friday Feb.3 2012 5:00:00 PM,
and get the present time, how to I get the spent time from the first date and the present and how do I get the remaining time from the present and the future date?
I have code to show but it is all wrong. There has to be a easy way to do it that I just cant see.
Thank you
Eric
You want to use functions from NSDate
for example:
//get time difference between someDate and now
NSTimeInterval diff = [someDate timeIntervalSinceNow];
//get difference between dates
NSTimeInterval diff2 =[someDate timeIntervalSinceDate: otherDate];
//comparing dates
NSDate * earlierDate = [someDate earlierDate: otherDate];
NSDate * laterDate = [someDate laterDate: otherDate];
If you have your dates available as NSDate objects, you can use timeIntervalSinceDate: to calculate the difference in seconds.
NSTimeInterval sinceThen = [firstDate timeIntervalSinceDate:[NSDate date]];
which will give you the time difference as an NSTimeInterval which is basically a double specifying the time in seconds. If the interval is negative, then firstDate is before now (which is the result of [NSDate date] otherwise it is in the future. If your date is not yet in NSDate form, you might employ an NSDateFormatter to do this (See here, parsing date strings).
I have an array which contains time in HH:mm:ss format.
I want to convert this into epoch time. To convert a particular time into epoch, date along with time should be passed.
I want to pass today's date along with the time from array.
For example 09:15:30 (date 22/12/2011) which is a string, should be converted into 1324525530000 (corresponding epoch value)
How should I convert this ??
Any help is appreciated.
Maybe you can get an idea looking at this snippet:
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] initWithSafeDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSTimeInterval epoch = [date timeIntervalSince1970];
Anyway you can have a look to NSDateFormatter and NSDate in the reference docs.
That looks like an epoch time in milliseconds - as far as I know it's supposed to be in seconds.
You can use NSDateFormatter to parse a string and convert it to a NSDate. This date can then return the seconds since 1970. If you really need it, simply multiply this by 1000.
In my app, I am getting a Date and start time of the same date and end time. The end time could be the time of very next day.
example : Date : 11/01/2011
start time : 5:30 PM
end time : 4:30 AM (of next day morning)
I am not getting the End Date.
How can I add one day into the start date based on the end time. I've referred many answers at SO but could not find the solution.
you should use this function
NSDate *endDate = [NSDate dateWithTimeIntervalSinceNow:86400];
here 86400 is 60(seconds)*60(minutes)*24(hours).If your startDate is not from current date then you can use this method
- (id)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)anotherDate
now you are getting like this
example : Date : 11/01/2011
start time : 5:30 PM
end time : 4:30 AM (of next day morning)
so you can have two dates, represents start time and end time. Use below method to get time interval
- (NSTimeInterval)timeIntervalSince1970
now you have two time interval for two different dates, right? Now simply subtract it and you will get what you want.
Solved : according to Akkis's answer...
NSDate *startDate = [df dateFromString:startingDateTime];
NSDate *endDate = [df dateFromString:endingDateTime];
NSComparisonResult result;
result = [startDate compare:endDate];
if(result == NSOrderedDescending)
{
NSLog(#"newDate is greater");
endDate = [endDate dateByAddingTimeInterval:60*60*24];
}
Dirty (NO DST check) method: It's easier if you convert everything to common units (i.e. NSTimeInterval / double seconds). Check if end time is less than start time (converted to seconds from midnight). Subtract startSeconds from endSeconds, if result is negative add 24 hours worth of seconds (24*60*60 = 86400). Add result to start date/time.