Convert mach absolute timestamp to NSDate - swift

I have a mach absolute timestamp from a sql database, I want to read in my app. I want to display this timestamp, so I want to convert it to NSDate. How is this possible? I have a timestamp like this: 443959550 (result in this case is UTC: 26.01.2015 10:05:50)

443959550 seems to be a time interval since the reference date
1 January 2001, GMT:
let date = NSDate(timeIntervalSinceReferenceDate: 443959550)
println(date) // 2015-01-26 10:05:50 +0000

Take a look at timeIntervalSince1970. There you can set a timestamp as init-parameter.
var timeStamp = 443959550
//NSDate
var date = NSDate(timeIntervalSince1970:443959550)

Related

Convert time AM and PM TO 24 hour format

String timer = "01:30 PM"
String = "12/10/2020"
DateTime fdate = DateFormat("yyyy-MM-dd").parse(dates);
DateTime ftime = DateFormat("hh:mm:ss").parse(timer);
I am getting the error while converting the string time into the Original time format. How to convert that time and date into the different format.
How to get the combination of date and time like 2020-10-12 13:30:00
Change ftime to :
DateTime ftime = DateFormat("HH:mm:ss").parse(timer);
Consider reading DateFormat Documentation for more information

how to convert a String to a Date without changing the Format

I'm using Xcode 11.4.1
In my Project I got a String with some data which I need to split up and save the Information in some variables.
now I face a problem if I try this
let dateString = "12-04-2020"
let dateFormatIn = DateFormatter()
dateFormatIn.dateFormat = "dd-MM-yyyy"
let saveDate: Date = dateFormatIn.date(from: dateString)!
print("The date is: \(saveDate)")
the result which I expected is "The date is: 12-04-2019" but what I got is "The date is: 2020-04-11 15:00:00 +0000"
what do I miss, what is this printed Date? it's not the current date ans also not the String!
I need the Date in the same format as I got it in the String.
A Date is a point in time, it doesn't have a format nor a time zone.
print shows the description – a string representation – of the date in the UTC time zone.
Your time zone is obviously UTC +0900, 2020-04-12 00:00:00 +0900 and 2020-04-11 15:00:00 +0000 is the same point in time.
If you want to create the date string independent of the time zone add the line
dateFormatIn.timeZone = TimeZone(secondsFromGMT: 0)
let saveDate: Date = dateFormatIn.date(from: dateString)!
The ": Date" looks like a data type declaration. You're saying, "create a variable called saveDate of type Date, and initialize it from the output of dateFormatIn.date(from: dateString)".
So you're not printing the formatted output. You're converting the formatted output to a new Date object, and then printing that.
Try:
let saveDate = dateFormatIn.date(from: dateString)!
This should save whatever dateFormatIn.date(from: dateString) returns in saveDate.

Convert from Unix Timestamp to date in Groovy

I have a date in unix timestamp and I want to convert it to human readable...
def dateUnix = 1486146877214
Date dateObj = new Date( ((long)dateUnix) * 1000 )
def cleanDate = new SimpleDateFormat('yyyy-MM-dd').format(dateObj)
println "clean date $cleanDate"
This gives me..
clean date 49064-02-13
where I was expecting "2017-02-03".
What am I doing wrong?
I even casted the timestamp to a long explicitly as suggested in this answer.
To convert Unix time Timestamp to Java Date:
def timestamp = 1486146877214 // milliseconds
def date = new Date( timestamp ).toString()
assert 'Fri Feb 03 13:34:37 EST 2017' == date.toString() // EN/US date format
Create timestamps and/or confirm dates at TimestampConvert.net or EpochConverter.com, etc.
Don't multiply by 1000 - new Date(long) is in millisecs not microsecs

What time/day format is openweathermap.org list.dt using

I'm using the day forecast api at http://openweathermap.org/forecast16. There is a parameter called list.dt. It gives a value in this format: 1406080800.
What format is that and how would I translate it to some human readable using functionality in Swift?
The date is most likely in unix time (see forecast5, which states that list.dt is the "Time of data forecasted, unix, UTC".
Using Swift, you can use NSDate(timeIntervalSince1970: dt) in order to convert this to a date. Then you can use standard NSDateFormatter to make it human readable.
The dt is for date and time.
The value of dt is in a format known as unix time
Unix time is a system for describing time, which is the number of seconds elapsed since 00:00:00 (UTC), Thursday, 1 January 1970.
The value you have there (1406080800) is equal to Wed, 23 Jul 2014 02:00:00 GMT which is used in there example provided for the link you posted.
To use within Swift try the following:
var date = NSDate(timeIntervalSince1970: timeInterval)
To get a unix timestamp from Swift you can also use:
let timeInterval = NSDate().timeIntervalSince1970
Create a computed property to convert timestamp that you've got into Date. CodingKeys are here just to rename the meaningless dt.
struct Weather: Codable {
private let timestamp: Double
let temp: Float
var date: Date {
Date(timeIntervalSince1970: timestamp)
}
enum CodingKeys: String, CodingKey {
case timestamp = "dt", temp
}
}

Timestamp to weekday going wrong

I made this function:
func getDayOfWeek(date: NSDate) -> String? {
let myCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let myComponents = myCalendar?.components(NSCalendarUnit.CalendarUnitWeekday, fromDate: date)
let weekDay = myComponents?.weekday
println(date)
return weekNumberToWord(weekDay!)
}
To which I give a timestamp as date (1439460000, which is the date of today) and it prints: 2046-08-13 10:00:00 +0000. How is this possible?
This is how I call the function:
var day = getDayOfWeek(NSDate(timeIntervalSinceReferenceDate: timestamp.doubleValue))!
In which getDayOfWeek() is a switch converting all the numbers to day name as string.
You are using the wrong initializer:
init(timeIntervalSinceReferenceDate:)
Returns an NSDate object initialized relative the first instant of 1 January 2001, GMT by a given number of seconds.
You should use init(timeIntervalSince1970 seconds: NSTimeInterval) to initialize a date from a UNIX timestamp (which you are apparently using):
Returns an NSDate object set to the given number of seconds from the first instant of 1 January 1970, GMT.
var day = getDayOfWeek(NSDate(timeIntervalSince1970: timestamp.doubleValue))!