Amit Pal
Amit Pal

Reputation: 11052

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 i.e. 1546903800000. Current date in epoch system will be Jan 7, 2018 00:00 AM. i.e 1546848000000

Upvotes: 9

Views: 18954

Answers (6)

vikingosegundo
vikingosegundo

Reputation: 52227

I use following Date extension to calculate today, tomorrow, yesterday, day after, day before, midnight and noon:

extension Date {
    static var     today: Date { Date().midnight  }
    static var yesterday: Date { .today.dayBefore }
    static var  tomorrow: Date { .today.dayAfter  }
    
    var dayBefore: Date { cal.date(byAdding:.day,value:-1,to:self)! }
    var  dayAfter: Date { cal.date(byAdding:.day,value: 1,to:self)! }
    var  midnight: Date { at(00,00)! }
    var      noon: Date { at(12,00)! }

    func at(_ h:Int,_ m:Int,_ s:Int = 0) -> Date? { cal.date(bySettingHour:h,minute:m,second:s,of:self) }
}

fileprivate var cal: Calendar { .autoupdatingCurrent }

https://gitlab.com/vikingosegundo/projectster/-/blob/main/Tools/Date.swift

Upvotes: 1

Manish
Manish

Reputation: 722

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())!
   }
}

Upvotes: 12

3ameration
3ameration

Reputation: 179

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

Upvotes: 0

kannan
kannan

Reputation: 364

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)!
    }
}

Upvotes: 5

rmaddy
rmaddy

Reputation: 318814

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

Upvotes: 27

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

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())

Upvotes: 19

Related Questions