Convert number to date time - ios

I'm not a programmer. I just needed to get some calendar info out of my iphone backup. I got the data out into a SQL Lite database but the dates are in a number format that I don't understand and I can't find a way to convert them to a format I can read. Can someone show me how to convert the numbers or it might be easier to just convert them for me. The numbers are:
352425600
353718000
356054400
357350400
358560000
359769600
Thanks

I wonder if these are in epoch time? try using this calculator and see if the results look possible to you.
EDIT:
nevermind, this answer has what you are looking for.

These numbers are stores as a time difference in seconds to the date 1. January 2001 GMT. You can calculate the date by adding the numbers to this date if you treat them as seconds.
If you can use Objective-C you can use the method: dateWithTimeIntervalSinceReferenceDate
Here is a short loop for calculating the dates:
NSArray *dates = #[#352425600, #353718000, #356054400, #357350400, #358560000, #359769600];
for (NSNumber *timeInterval in dates) {
NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate:[timeInterval integerValue]];
NSLog(#"timeInterval:%# = date:%# ", timeInterval, date);
}
And here is the result:
timeInterval:352425600 = date:2012-03-03 00:00:00 +0000
timeInterval:353718000 = date:2012-03-17 23:00:00 +0000
timeInterval:356054400 = date:2012-04-14 00:00:00 +0000
timeInterval:357350400 = date:2012-04-29 00:00:00 +0000
timeInterval:358560000 = date:2012-05-13 00:00:00 +0000
timeInterval:359769600 = date:2012-05-27 00:00:00 +0000

Related

Swift comparing date is not working and getting wrong time

Hi I have multiple dates which I am comparing with current date. But it is not working. I have done coding but nothing is working for me. I am using comparison same like this link.
Here is what I am doing
let todayDate = Date()
let date1FromServer = //its Date from server which is 2020-02-01 09:00:00 +0000
let date2FromServer = //its Date from server which is 2020-02-02 09:00:00 +0000
let date3FromServer = //its Date from server which is 2020-02-03 09:00:00 +0000
now here I am comparing All three date in simple if else
if(date1FromServer < todayDate){
//Do something for date 1
}else if (date2FromServer < todayDate){
//Do something for date 3
}else if (date3FromServer < todayDate){
// Do something for date 3
}
Case: Now I have date1 and Date 2 is working but date3 is not working. I am saying this because the date3 is exactly on 3 feb 2020
with 9:00 am which is smaller then today date which is 3 feb 2020 with
11:00 am , but it looks like that it is dealing like date 3 is equal
or greater then today date.
When I print date 3 it prints (2020-02-03 06:32:22 +0000) and here the time is wrong, as right now on my device it si 11:32 am but it showing 06: 32 . I think as I am just printing it in log so there could be problem of time zone, but on above code it is also not working.
Please let me know what is the problem here.
Note: I want to compare the Date and time as well.....
If your server date has GMT as time zone then you can adjust your local date to GMT by using offsets
let offsetDate = Date(timeIntervalSince1970:
todayDate.timeIntervalSince1970 + Double(TimeZone.current.secondsFromGMT(for: todayDate)))
and then use offsetDate instead when doing the comparison

CMMotionActivity startDate is 1970 not 2016

My CMMotionActivity object has the property timestamp and startDate.
Printing both of these shows the value is from 1970, not 2016 as expected.
[self.motionActivityManager startActivityUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMMotionActivity *activity) {
NSLog(#"startDate = %#",activity.startDate);
NSLog(#"timestamp = %f",activity.timestamp);
}];
2016-07-20 17:46:53.294 MyApp[292:30246] timestamp = 8648.852289
2016-07-20 17:46:54.229 MyApp[292:30246] startDate = 1970-01-01 02:24:09 +0000
My device's time and date is set correctly, though it has been turned off for weeks.
EDIT: Today I got this: No code changes.
2016-07-21 11:26:34.292 MyApp[229:7473] startDate = 2016-07-21 10:26:34 +0000
2016-07-21 11:26:34.293 MyApp[229:7473] timestamp = 662.524030
Eh... Close enough. "timestamp" (from NSLogItem) is a NSTimeInterval so a phone running time or something.
I guess 24hrs or a phone reboot fixes the issue.

How to convert a milliseconds timestamp to date and time in iOS/XCode?

I have a timestamp measured in milliseconds since January 1, 1970, 00:00:00 UTC.
eg. input "takeoffTime": "1396614600000"
output in date format
You can do this by doing the following:
[NSDate dateWithTimeIntervalSince1970:1396614600000/1000];
This will give you 2014-04-04 12:30:00 +0000. You should read up on how to pass messages (call methods) here: How can I call a method in Objective-C?
The NSDate class gives you this method + (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)seconds.
Look here: Apple docs
NSString* takeOffTime = #"1396614600000";
double miliSec = takeOffTime.doubleValue;
NSDate* takeOffDate = [NSDate dateWithTimeIntervalSince1970:miliSec/1000];

Issues in converting date string (any time zone) to NSDate of French Time Zone

I want to convert a date string (can be in any time zone) to a date in French Time Zone. I am using following code.
NSString * dateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* frenchDateFormatter = [[NSDateFormatter alloc] init];
[frenchDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[frenchDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *frenchDate = [frenchDateFormatter dateFromString:dateString];
NSLog(#"%#",frenchDate);
NSString * frenchString = [frenchDateFormatter stringFromDate:frenchDate];`
Elaboration
--> System time zone is GMT +5
--> French time zone is GMT +2
Date string = 27/05/2015 - 19:00
Expected result = 27/05/2015 - 16:00
Actual result (NSDate) = 2015-05-27 17:00:00 +0000
Actual result (NSString from date) = 27/05/2015 - 19:00
Kindly point out if I am missing something
If you use NSLog to display dates it'll be displayed in UTC. So either you have to convert in your head, or don't use it. I wrote a long answer explaining this to a different question.
Because you have set the timezone of your parsing dateFormatter to Paris the string you parse is treated as "time in paris". That's your problem, you actually wanted to parse it in local time.
The results you get are exactly as one would expect.
You create a NSDate that relates to "19:00 in Paris". Since Paris is UTC+2 that date is 17:00 in UTC (or in +0000). If you convert that date back to "time in Paris" you end up with the same string as before.
If you want to convert the representation of a point in time in your location to a different representation at a different location you have to use two dateFormatters.
NSString *localDateString = #"27/05/2015 - 19:00" // system time zone is GMT +5
NSDateFormatter* localDateFormatter = [[NSDateFormatter alloc] init];
[localDateFormatter setTimeZone:[NSTimeZone localTimeZone]];
[localDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSDate *date = [localDateFormatter dateFromString:localDateString]; // date contains point in time. It no longer has a timezone
NSDateFormatter* franceDateFormatter = [[NSDateFormatter alloc] init];
[franceDateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"Europe/Paris"]];
[franceDateFormatter setDateFormat:#"dd/MM/yyyy - HH:mm"];
NSString * timeInFranceString = [franceDateFormatter stringFromDate:date]; // representation of the point in time from above for people in Paris
This line prints out the date/time in GMT, as it calls [NSDate description], and there is a potential difference between systemTimeZone and GMT, hence the difference you are seeing:
NSLog(#"%#",currentDate);
If you want to see what the date/time is for a particular timezone then use the NSDateFormatter object to get the string.
A date doesn't have a time zone information. A date is internally represented as a number. We don't have to know anything about that number (it's a number of seconds from a fixed date in UTC), the important thing is to understand that to display a date to a user, you have to convert it to a string first.
A string representation of a number is generated from a date using a date format and a time zone. For all date -> string and string -> date conversions you can use NSDateFormatter.
You have successfully parsed currentDate from your string representation. If you want to reverse the process and get the string representation, just use [currentDateFormatter stringFromDate:currentDate]
Check at http://www.timeanddate.com/worldclock/
Right now Paris is two hours ahead of UTC. The result is absolutely correct. NSDate keeps dates in UTC. The idea is that if any two people look at their watch at the same moment, and convert the time they see on their watch to NSDate, they will get the same result.
You cannot get an NSDate for a timezone. NSDate doesn't support time zones. The only way to get a date with a time zone is to use NSDateFormatter to convert it to a string.

NSDateformatter does not provide correct date swift

When I am using NSDateformatter to get date from string it returns wrong value.I dont know what must be reason.Please help me to resolve.
Thanks in advance.
Input String:-
newDate "06:11"
end time "07:00"
start time "05:00"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm"
let date:NSDate = dateFormatter.dateFromString(dateString)!
println(date)
Output Date:-
current date 2000-01-01 00:41:00 +0000
end date 2000-01-01 01:30:00 +0000
start date 1999-12-31 23:30:00 +0000
By doing
println(date)
You are printing the description of the date, which is equal to -
println(date.description())
And it always prints the time in UTC (GMT)
So if you are in UTC+5:30, it will display time that is before 5:30 hours than your entered time. If you want to print date in your timezone, use a NSDateFormatter again. To see date of other time zone, set NSDateFormatter timeZone.
Hope this explains.

Resources