Big int to date time format in swift [duplicate] - swift

This question already has answers here:
How to Convert UNIX epoch time to Date and time in ios swift
(3 answers)
Closed 2 years ago.
I am doing this in swift:
let date = NSDate(timeIntervalSince1970: 1432233446145.0)
println("date is \(date)")
The log gives me this:
date is 47355-09-02 23:55:45 +0000
Then I try to get an interval back out just for testing:
let tI = expirationDate.timeIntervalSinceDate(date)
println("tI = \(tI)")
I get:
tI = 0.0
What am I doing wrong?
I can seem to make the timeIntervalSince1970 call work properly. Is there any known issued with that in Swift, or am I missing something here?

1432233446145 most probably is a time interval given in milliseconds:
let date = NSDate(timeIntervalSince1970: 1432233446145.0/1000.0)
print("date is \(date)")
// date is 2015-05-21 18:37:26 +0000
Swift 3 and later:
let date = Date(timeIntervalSince1970: 1432233446145.0/1000.0)

I think your timestamp is incorrect. This results in your date being september 2nd, 47355.
If I use the following I get the date for (right about) now:
let date = NSDate(timeIntervalSince1970: 1431024488)
println("date is \(date)")
// "date is 2015-05-07 18:48:08 +0000"
The printed date is not a localized timestamp, so you'll have to do some localization of your own I suppose. An example:
let formatter = NSDateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
println("formatted date is \(formatter.stringFromDate(date))")
// "formatted date is 07-05-2015 20:48:08"
And for completeness I also checked with an expiration date that's 1100 seconds larger than the initial date:
let expirationDate = NSDate(timeIntervalSince1970: 1431025588)
let diff = expirationDate.timeIntervalSinceDate(date)
println("expires in: \(diff)")
// "expires in: 1100.0"
So, the timeIntervalSince1970 seems to work fine, I think your interval was just not what you wanted it to be.

From your code and the log content follows:
date.isEqual(expirationDate)
--> Your stuff has just expired :-).

Related

Getting start of month and end of month with time for December [duplicate]

This question already has answers here:
NSDateFormatter show wrong year
(1 answer)
first and last day of the current month in swift
(11 answers)
Getting back a date from a string
(3 answers)
Closed 2 years ago.
As the title indicates I'm trying to get the start and end of month with a time component for December. I'm sure I'm missing something obvious related to TimeZones but I can't seem to get a same year for both dates in in PST time zone.
Here's how I get the start of month and day:
NOTE: If you try this in the swift REPL there's a bug that shows nil result even though the
variables have a non-nil value, use print or debugPrint to show the value.
let cal = Calendar.current
let startOfMonth = cal.startOfDay(for: cal.date(from: DateComponents(year: 2020, month: 12, day: 1))!)
Here's how I calculate the end of day up to seconds:
let startOfNextMonth = cal.date(byAdding: DateComponents(month: 1), to: startOfMonth)!
let endOfMonth = cal.date(byAdding: DateComponents(second: -1), to: startOfNextMonth)!
By this point startOfMonth has the value: Optional(2020-12-01 08:00:00 +0000) and endOfMonth has a value of Optional(2021-01-01 07:59:59 +0000)
Which seems fine and dandy because times are printed in GMT (UTC) and if I subtract 8 hours endOfMonth it would end up like Optional(2020-12-31 23:59:59 +0000)
However the trouble comes when formatting the endOfMonth date, even If I set the TimeZone to "America/Los_Angeles" I still get the year value as 2021 and not as 2020, here's how I create my formatter:
let fmt = DateFormatter()
fmt.timeZone = .current // "America/Los_Angeles"
fmt.dateFormat = "d/Y"
fmt.string(from: endOfMonth!) // $R13: String = "31/2021"
What's going on?, wouldn't conversion to the default Time zone yield the year 2020?
After setting the formatter dateStyle property to .long and obtaining the correct year printed (though not the format I wanted) I realized that the issue might be in the dateFormat.
After reading a bit more of the documentation, I found a link to the format specifiers
Date Format Swift Docs
Date Formatting Guide
Unicode Date Format Patterns
Eventually I found out that the Y specifier is essentially a "Week of Year" vs the y specifier which is "Year of Era".
Using the following fixed my issue:
let formatter = DateFormatter()
formatter.dateFormat = "d/y"
formatter.string(from: endOfMonth!) // $R1: String = "31/2020"

DateFormatter displaying Date Wrongly in iPhone device [duplicate]

This question already has answers here:
Getting date from [NSDate date] off by a few hours
(3 answers)
Closed 5 years ago.
I did an extension for Date that returns a formatted string:
extension Date {
var myFormattedDate : String {
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.dateFormat = "EEEE, MMMM d, y (HH:mm a)"
return formatter.string(for: self)!
}
}
On runtime, I set a breakpoint inside the myFormattedDate property.
po self printed:
2017-09-05 08:50:00 +0000
po formatter.string(for: self)! printed:
Tuesday, September 5, 2017 (11:50 AM)"
What could be the problem?
Thanks!
Printing a Date always returns an UTC time, regardless of the local time zone. Just avoid printing a Date object directly if you want to see the date with the proper time zone in your console.

Swift language bug in DateFormatter? [duplicate]

This question already has answers here:
1st april dates of 80s failed to parse in iOS 10.0
(1 answer)
Why NSDateFormatter is returning null for a 19/10/2014 in a Brazilian time zone?
(3 answers)
DateFormatter's returns nil for specific date strings without time in Swift
(1 answer)
Closed 5 years ago.
I was testing an app that has an UITextField where users enter a date, so I take the entered string and use a DateFormatter to generate a Date object.
The problem first occurred when I tried converting the string "10/15/2017" which returned a nil value. So I create a code to generate string from "01/01/2000" to "12/31/2020" and I realized that the problem is occurring in all years around October or November of each year.
I created a code to print all dates that return a nil value:
import UIKit
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.locale = Locale(identifier: "en_US")
for day in 1...30 { // Generate the days
for month in 1...12 { // Generate the months
for year in 2000...2020 {
if month == 2, day > 28 { // Check if month is february
continue
}
str = "\(String(format: "%02d", month))/\(String(format: "%02d", day))/\(String(format: "%02d", year))"
let date = dateFormatter.date(from: str)
if date == nil {
print("\(str)")
}
}
}
}
I also tried changing the dateFormat, or even the locale properties and I'm also getting nil for some entries.
dateFormatter.dateFormat = "MM/dd/yyyy"
This snippet of code prints the following:
11/02/2004
11/03/2002
11/05/2006
10/08/2000
10/14/2001
10/14/2007
10/15/2017
10/16/2005
10/16/2011
10/16/2016
10/17/2010
10/18/2009
10/18/2015
10/18/2020
10/19/2003
10/19/2008
10/19/2014
10/20/2013
10/20/2019
10/21/2012
10/21/2018
I'm using Xcode 8.3.3 and Swift 3. Is that a bug with Swift/Xcode or I'm doing something wrong?
It's most likely the Brazil-Fall-Syndrome
The daylight saving changes in Brazil occur at midnight. In fall, when the clock is set forward, there is no 12:00 am / 0:00 so the date is nil.
Set the time zone to a country where daylight saving changes don't occur at midnight or set the hour to noon

NSDate timeIntervalSince1970 not working in Swift? [duplicate]

This question already has answers here:
How to Convert UNIX epoch time to Date and time in ios swift
(3 answers)
Closed 2 years ago.
I am doing this in swift:
let date = NSDate(timeIntervalSince1970: 1432233446145.0)
println("date is \(date)")
The log gives me this:
date is 47355-09-02 23:55:45 +0000
Then I try to get an interval back out just for testing:
let tI = expirationDate.timeIntervalSinceDate(date)
println("tI = \(tI)")
I get:
tI = 0.0
What am I doing wrong?
I can seem to make the timeIntervalSince1970 call work properly. Is there any known issued with that in Swift, or am I missing something here?
1432233446145 most probably is a time interval given in milliseconds:
let date = NSDate(timeIntervalSince1970: 1432233446145.0/1000.0)
print("date is \(date)")
// date is 2015-05-21 18:37:26 +0000
Swift 3 and later:
let date = Date(timeIntervalSince1970: 1432233446145.0/1000.0)
I think your timestamp is incorrect. This results in your date being september 2nd, 47355.
If I use the following I get the date for (right about) now:
let date = NSDate(timeIntervalSince1970: 1431024488)
println("date is \(date)")
// "date is 2015-05-07 18:48:08 +0000"
The printed date is not a localized timestamp, so you'll have to do some localization of your own I suppose. An example:
let formatter = NSDateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
println("formatted date is \(formatter.stringFromDate(date))")
// "formatted date is 07-05-2015 20:48:08"
And for completeness I also checked with an expiration date that's 1100 seconds larger than the initial date:
let expirationDate = NSDate(timeIntervalSince1970: 1431025588)
let diff = expirationDate.timeIntervalSinceDate(date)
println("expires in: \(diff)")
// "expires in: 1100.0"
So, the timeIntervalSince1970 seems to work fine, I think your interval was just not what you wanted it to be.
From your code and the log content follows:
date.isEqual(expirationDate)
--> Your stuff has just expired :-).

How to Convert UNIX epoch time to Date and time in ios swift

I'm trying to convert the UNIX epoc time to datetime format using the below code
var epocTime = NSTimeInterval(1429162809359)
let myDate = NSDate(timeIntervalSince1970: epocTime)
println("Converted Time \(myDate)")
the actual result is (Thu, 16 Apr 2015 05:40:09 GMT) but am getting something like (47258-05-14 05:15:59 +0000) Can anyone please tel me how to achieve this.
update: Xcode 8.2.1 • Swift 3.0.2 or later
You need to convert it from milliseconds dividing it by 1000:
let epochTime = TimeInterval(1429162809359) / 1000
let date = Date(timeIntervalSince1970: epochTime) // "Apr 16, 2015, 2:40 AM"
print("Converted Time \(date)") // "Converted Time 2015-04-16 05:40:09 +0000\n"
Swift 5
I am dealing with a date in a JSON api which is defined as an Int and an example of the timestamp is 1587288545760 (UTC)
The only way I could display that value as a Date in a way that made any sense was to truncate the last 3 digits and convert THAT resultant date to "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
This was the function I created to achieve that.
func convertDate(dateValue: Int) -> String {
let truncatedTime = Int(dateValue / 1000)
let date = Date(timeIntervalSince1970: TimeInterval(truncatedTime))
let formatter = DateFormatter()
formatter.timeZone = TimeZone(abbreviation: "UTC")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
return formatter.string(from: date)
}
It works for me and I end up with a date that looks like this:
"2020-04-19T09:29:05.000Z"
..and it reflects the fact that the original time stamp is exactly that date.
Hope that helps anyone having the same issue.
It seems that your time information is "milliseconds since 1970". Should have been straightforward to figure out: We are about 46 years after 1970, but your date is about 45,000 after 1970.
NSTimeInterval is based on SECONDS.
Convert your number to double and divide by 1000.0.