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

6

9

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

Stepheniestephens answered 8/1, 2019 at 1:10 Comment(7)
What’s different from the value you get from the value you want?Monamonachal
Date().timeIntervalSince1970 - should be the number of seconds from the unix epochSidky
Possible duplicate of Get Unix Epoch Time in SwiftHurrah
How to add days to a date in swift 3 should give you starting point for the second part .. and you shouldn't be using Calendar in Java anymore eitherSidky
@Sidky This will add the today's time from 12 AM. I just want the date which is today's date @ 00:00:00 . (hour: minute: second)Stepheniestephens
@AmitPal And what makes you think you'd use a different approach? Start with the provided examples and consult the documentation. You question is to broad and is not that uncommon as to not be answered before, you will need to make a further attemptsSidky
@Sidky As I mentioned Date(). timeIntervalSince1970 will give you the current time since 1970. I need today's date (12 AM) only since 1970Stepheniestephens
R
18

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())
Recapitulate answered 8/1, 2019 at 1:43 Comment(2)
Thanks for the answer. Could you please multiply timeIntervalSince1970 with 1000 for the full fledge solutionStepheniestephens
It's all in what you want I suppose. time(2) is in seconds, but time(3) is in milliseconds. I'm old, so I assumed time(2).Recapitulate
M
26

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
Monamonachal answered 8/1, 2019 at 3:5 Comment(0)
R
18

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())
Recapitulate answered 8/1, 2019 at 1:43 Comment(2)
Thanks for the answer. Could you please multiply timeIntervalSince1970 with 1000 for the full fledge solutionStepheniestephens
It's all in what you want I suppose. time(2) is in seconds, but time(3) is in milliseconds. I'm old, so I assumed time(2).Recapitulate
H
13

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())!
   }
}
Hight answered 9/3, 2021 at 12:31 Comment(0)
A
5

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)!
    }
}
Abdomen answered 8/1, 2019 at 5:19 Comment(0)
D
1

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
Drexler answered 8/1, 2020 at 17:44 Comment(0)
P
1

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 { Date.today.dayBefore }
    static var  tomorrow: Date { 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 { Calendar.autoupdatingCurrent }
Picked answered 8/9, 2024 at 17:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.