Get build date and time in Swift
You can use #line
, #column
, and #function
.
Original answer:
Create a new Objective-C file in your project, and when Xcode asks, say yes to creating the bridging header.
In this new Objective-C file, add the following the the .h
file:
NSString *compileDate();
NSString *compileTime();
And in the .m
implement these functions:
NSString *compileDate() {
return [NSString stringWithUTF8String:__DATE__];
}
NSString *compileTime() {
return [NSString stringWithUTF8String:__TIME__];
}
Now go to the bridging header and import the .h
we created.
Now back to any of your Swift files:
println(compileDate() + ", " + compileTime())
Showing App's Build Date
Try running the script as a build phase step, rather than a scheme pre-action step, so it's run all the time, regardless of the type of build you are producing.
How to get the current time as datetime
Update for Swift 3:
let date = Date()
let calendar = Calendar.current
let hour = calendar.component(.hour, from: date)
let minutes = calendar.component(.minute, from: date)
I do this:
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: date)
let hour = components.hour
let minutes = components.minute
See the same question in objective-c How do I get hour and minutes from NSDate?
Compared to Nate’s answer, you’ll get numbers with this one, not strings… pick your choice!
How to get iOS APP archive date using Swift
You can get the url of your app using Bundle property executableURL and use url method resourceValues to get the bundle creation date:
if let executableURL = Bundle.main.executableURL,
let creation = (try? executableURL.resourceValues(forKeys: [.creationDateKey]))?.creationDate {
print(creation)
}
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))
How do you create a Swift Date object?
Swift has its own Date
type. No need to use NSDate
.
Creating a Date and Time in Swift
In Swift, dates and times are stored in a 64-bit floating point number measuring the number of seconds since the reference date of January 1, 2001 at 00:00:00 UTC. This is expressed in the Date
structure. The following would give you the current date and time:
let currentDateTime = Date()
For creating other date-times, you can use one of the following methods.
Method 1
If you know the number of seconds before or after the 2001 reference date, you can use that.
let someDateTime = Date(timeIntervalSinceReferenceDate: -123456789.0) // Feb 2, 1997, 10:26 AM
Method 2
Of course, it would be easier to use things like years, months, days and hours (rather than relative seconds) to make a Date
. For this you can use DateComponents
to specify the components and then Calendar
to create the date. The Calendar
gives the Date
context. Otherwise, how would it know what time zone or calendar to express it in?
// Specify date components
var dateComponents = DateComponents()
dateComponents.year = 1980
dateComponents.month = 7
dateComponents.day = 11
dateComponents.timeZone = TimeZone(abbreviation: "JST") // Japan Standard Time
dateComponents.hour = 8
dateComponents.minute = 34
// Create date from components
let userCalendar = Calendar(identifier: .gregorian) // since the components above (like year 1980) are for Gregorian
let someDateTime = userCalendar.date(from: dateComponents)
Other time zone abbreviations can be found here. If you leave that blank, then the default is to use the user's time zone.
Method 3
The most succinct way (but not necessarily the best) could be to use DateFormatter
.
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd HH:mm"
let someDateTime = formatter.date(from: "2016/10/08 22:31")
The Unicode technical standards show other formats that DateFormatter
supports.
Notes
See my full answer for how to display the date and time in a readable format. Also read these excellent articles:
- How to work with dates and times in Swift 3, part 1: Dates, Calendars, and DateComponents
- How to work with dates and times in Swift 3, part 2: DateFormatter
- How to work with dates and times in Swift 3, part 3: Date arithmetic
is there is a way to show old date and time picker in swift
From iOS 13.4, Apple change the way it show picker.
To show date picker in old way, just add below lines in the code.
if #available(iOS 13.4, *) {
self.preferredDatePickerStyle = .wheels
}
Note : You have to write this line right after you initialize date picker.
E.x. If you write this line after setting maximum or minimum date, it won't work like below.
if #available(iOS 13.4, *) {
self.preferredDatePickerStyle = .wheels
}
self.minimumDate = Date()
Related Topics
Programmatically Creating Constraints Bound to View Controller Margins
How Set Rootviewcontroller in Scene Delegate iOS 13
Implementing Nscopying in Swift with Subclasses
Grab Frames from Video Using Swift
Checking If Textfields Are Empty Swift
"Cannot Assign Value of Type 'String' to Type 'Anyobject'", Swift 3, Xcode 8 Beta 6
How to Change the Associated Values of a Enum
In Swiftui, Where Are the Control Events, I.E. Scrollviewdidscroll to Detect the Bottom of List Data
Get Image from Calayer or Nsview (Swift 3)
Background Request Not Execute Alamofire Swift
Back Button Image - What Is It Called in Swift
What Are 'Get' and 'Set' in Swift
Select Multiple Rows in Tableview and Tick the Selected Ones
Lazy Loading Properties in Swift
Today Extension Failed to Inherit Coremedia Permissions From