简体   繁体   中英

How to get the today's and tomorrow's date in swift 4

How to get the current date in unix-epoch ? timeIntervalSince1970 prints the current time. Is there any way to get today's time at 12 AM?

For example, The current time is : Jan 7, 2018 5:30 PM. timeIntervalSince1970 will print the current time ie 1546903800000 . Current date in epoch system will be Jan 7, 2018 00:00 AM. ie 1546848000000

This can be done very simply using the following code. No need for date components or other complications.

var calendar = Calendar.current
// Use the following line if you want midnight UTC instead of local time
//calendar.timeZone = TimeZone(secondsFromGMT: 0)
let today = Date()
let midnight = calendar.startOfDay(for: today)
let tomorrow = calendar.date(byAdding: .day, value: 1, to: midnight)!

let midnightEpoch = midnight.timeIntervalSince1970
let tomorrowEpoch = tomorrow.timeIntervalSince1970

I would do this with components.

Assuming you need time in seconds as defined by time(2) . If you need in milliseconds as defined by time(3) , then you can multiply it out by 1000.

// Get right now as it's `DateComponents`.
let now = Calendar.current.dateComponents(in: .current, from: Date())

// Create the start of the day in `DateComponents` by leaving off the time.
let today = DateComponents(year: now.year, month: now.month, day: now.day)
let dateToday = Calendar.current.date(from: today)!
print(dateToday.timeIntervalSince1970)

// Add 1 to the day to get tomorrow.
// Don't worry about month and year wraps, the API handles that.
let tomorrow = DateComponents(year: now.year, month: now.month, day: now.day! + 1)
let dateTomorrow = Calendar.current.date(from: tomorrow)!
print(dateTomorrow.timeIntervalSince1970)

You can get yesterday by subtracting 1.


If you need this in the universal time (UTC, GMT, Z… whatever name you give universal time), then use the following.

let utc = TimeZone(abbreviation: "UTC")!
let now = Calendar.current.dateComponents(in: utc, from: Date())

Use this extension to get today's and tomorrow's date

extension Date {
   static var tomorrow:  Date { return Date().dayAfter }
   static var today: Date {return Date()}
   var dayAfter: Date {
      return Calendar.current.date(byAdding: .day, value: 1, to: Date())!
   }
}

Also try adding following code in date extension:

extension Date
{
    var startOfDay: Date 
    {
        return Calendar.current.startOfDay(for: self)
    }

    func getDate(dayDifference: Int) -> Date {
        var components = DateComponents()
        components.day = dayDifference
        return Calendar.current.date(byAdding: components, to:startOfDay)!
    }
}

You can use the following method to get any date by adding days or months or years by specifying the Calendar Component and the increment value of this component:

func getSpecificDate(byAdding component: Calendar.Component, value: Int) -> Date {

    let noon = Calendar.current.date(bySettingHour: 12, minute: 0, second: 0, of: self)!

    return Calendar.current.date(byAdding: component, value: value, to: noon)!
}

Where the component wil be one from the following option : ( .day , .month , .year ) and the value will be the amount you want to add for this component

for example to get the next year date you can use the following code:

var nextYear = getSpecificDate(byAdding: .year, value: 1).timeIntervalSince1970

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM