How to get 18-digit current timestamp in Swift?
You seem be looking for what DateTime.Ticks
is in C#, i.e. the
time since 0001-01-01 measured in 100-nanosecond intervals.
The code from your provided link Swift: convert NSDate to c# ticks can be translated to Swift easily:
// Swift 2:
extension NSDate {
var ticks: UInt64 {
return UInt64((self.timeIntervalSince1970 + 62_135_596_800) * 10_000_000)
}
}
// Swift 3:
extension Date {
var ticks: UInt64 {
return UInt64((self.timeIntervalSince1970 + 62_135_596_800) * 10_000_000)
}
}
Example (Swift 3):
let ticks = Date().ticks
print(ticks) // 636110903202288256
or as a string:
let sticks = String(Date().ticks)
print(sticks)
And while are are at it, the reverse conversion from ticks to Date
would be
// Swift 2:
extension NSDate {
convenience init(ticks: UInt64) {
self.init(timeIntervalSince1970: Double(ticks)/10_000_000 - 62_135_596_800)
}
}
// Swift 3:
extension Date {
init(ticks: UInt64) {
self.init(timeIntervalSince1970: Double(ticks)/10_000_000 - 62_135_596_800)
}
}
Example (Swift 3):
let date = Date(ticks: 636110903202288256)
iOS Swift - Get the Current Local Time and Date Timestamp
For saving Current time to firebase database I use Unic Epoch Conversation:
let timestamp = NSDate().timeIntervalSince1970
and For Decoding Unix Epoch time to Date().
let myTimeInterval = TimeInterval(timestamp)
let time = NSDate(timeIntervalSince1970: TimeInterval(myTimeInterval))
Date to milliseconds and back to date in Swift
I don't understand why you're doing anything with strings...
extension Date {
var millisecondsSince1970:Int64 {
Int64((self.timeIntervalSince1970 * 1000.0).rounded())
}
init(milliseconds:Int64) {
self = Date(timeIntervalSince1970: TimeInterval(milliseconds) / 1000)
}
}
Date().millisecondsSince1970 // 1476889390939
Date(milliseconds: 0) // "Dec 31, 1969, 4:00 PM" (PDT variant of 1970 UTC)
Get Unix Epoch Time in Swift
You can simply use NSDate's timeIntervalSince1970
function.
let timeInterval = NSDate().timeIntervalSince1970
Add next week (7 days) from current var (Int) to 10 digit unix timestamp in swiftui
Depending on the accuracy you want either add 7 * 24 * 60 to your timestamp as suggested in the comments is one way.
Note you could also create a date from the timestamp and use the appropriate date functions as described here: How do I add 1 day to an NSDate?
if your loginUpdateDate is a Swift Date, just use the code in the link. Otherwise use the timestamp (the 10 digit int) to create a date:
let lastLoginDate = Date(timeIntervalSince1970: lastLoginUpdate)
var dayComponent = DateComponents()
dayComponent.day = 1 // For removing one day (yesterday): -1
let theCalendar = Calendar.current
let nextAllowedDate = theCalendar.date(byAdding: dayComponent, to: lastLoginDate)
iPhone: How to get current milliseconds?
[[NSDate date] timeIntervalSince1970];
It returns the number of seconds since epoch as a double. I'm almost sure you can access the milliseconds from the fractional part.
Get current time as string swift 3.0
This is the way I figure it out.
// Get today date as String
func getTodayString() -> String{
let date = Date()
let calender = Calendar.current
let components = calender.dateComponents([.year,.month,.day,.hour,.minute,.second], from: date)
let year = components.year
let month = components.month
let day = components.day
let hour = components.hour
let minute = components.minute
let second = components.second
let today_string = String(year!) + "-" + String(month!) + "-" + String(day!) + " " + String(hour!) + ":" + String(minute!) + ":" + String(second!)
return today_string
}
let today : String!
today = getTodayString()
What is the best way to deal with the NSDateFormatter locale feature?
Duh!!
Sometimes you have an "Aha!!" moment, sometimes it's more of a "Duh!!" This is the latter. In the category for initWithSafeLocale
the "super" init
was coded as self = [super init];
. This inits the SUPERCLASS of NSDateFormatter
but does not init
the NSDateFormatter
object itself.
Apparently when this initialization is skipped, setLocale
"bounces off", presumably because of some missing data structure in the object. Changing the init
to self = [self init];
causes the NSDateFormatter
initialization to occur, and setLocale
is happy again.
Here is the "final" source for the category's .m:
#import "NSDateFormatter+Locale.h"
@implementation NSDateFormatter (Locale)
- (id)initWithSafeLocale {
static NSLocale* en_US_POSIX = nil;
self = [self init];
if (en_US_POSIX == nil) {
en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
}
[self setLocale:en_US_POSIX];
return self;
}
@end
Once a 13 digit timestamp is saved in firebase, how can it be fetched and converted into a date in Xcode?
Since you listen to UserS->caption
then snapshot.value
is an Int
. so you need
databaseRef.child("UserS").child(uid).child("caption").observeSingleEvent(of: .value, with: { (snapshot) in
guard let caption = snapshot.value as? Int else { return }
print(caption)
let date = Date(timeIntervalSince1970: TimeInterval(caption)/1000.0)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss.SSSSxxx"
let res = dateFormatter.string(from: date)
print(res)
}
Edit:
if Calendar.current.dateComponents([.day], from:date, to: Date()).day! > 0 {
/// at least 1 day passed
}
Converting 12 digit ticks to Date
Just don't subtract 62_135_596_800
extension Date {
init(ticks: UInt64) {
self.init(timeIntervalSince1970: Double(ticks)/10_000_000)
}
}
1970-01-01 18:00:00 +0000
The other problem: When you create date
and print it, the string is formatted in UTC time zone (offset GMT+0). But DateFormatter
returns string representation dependent on its time zone, which is the local timezone by default.
You can fix your code just by setting dateFormatter
's timeZone
to UTC
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
18:00
Related Topics
Universal Links Not Working on iOS10
Uitableviewcellaccessorycheckmark and Autolayout Constraints
How to Sort Nsmutablearray of Date Objects
Where Does a Swift iOS Application Begin Its Life
Firebase Pod Install - Pod 'Firebase/Database' - Required a Higher Minimum Deployment Target
Audiokit iOS Aksamplermetronome
Swift: Change Status Bar Color for iOS 13
Using iOS 6 Theme for iOS 7 App
Swiftui: Animation Inside Navigationview
Setting Up a Plist to Store Application Data (Not Settings) for an iPhone Game
How to Control Avassetwriter to Write at the Correct Fps
How to Find Out What View a Touch Event Ended At
How to Properly Remove Node When Out of Screen Bounds
Authentication with Wkwebview in Swift
Facebook Registration:The Operation Couldn't Be Completed (Com.Facebook.Sdk Error 2)