6

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

Amit Pal
  • 10,604
  • 26
  • 80
  • 160
  • What’s different from the value you get from the value you want? – rmaddy Jan 08 '19 at 01:12
  • `Date().timeIntervalSince1970` - should be the number of seconds from the unix epoch – MadProgrammer Jan 08 '19 at 01:13
  • Possible duplicate of [Get Unix Epoch Time in Swift](https://stackoverflow.com/questions/25096602/get-unix-epoch-time-in-swift) – esqew Jan 08 '19 at 01:14
  • [How to add days to a date in swift 3](https://stackoverflow.com/questions/45570546/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 either – MadProgrammer Jan 08 '19 at 01:14
  • @MadProgrammer 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) – Amit Pal Jan 08 '19 at 01:17
  • @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 attempts – MadProgrammer Jan 08 '19 at 01:19
  • @MadProgrammer As I mentioned `Date(). timeIntervalSince1970 ` will give you the current time since 1970. I need today's date (12 AM) only since 1970 – Amit Pal Jan 08 '19 at 01:23

5 Answers5

24

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
rmaddy
  • 314,917
  • 42
  • 532
  • 579
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())
Jeffery Thomas
  • 42,202
  • 8
  • 92
  • 117
  • Thanks for the answer. Could you please multiply `timeIntervalSince1970 ` with `1000` for the full fledge solution – Amit Pal Jan 08 '19 at 01:48
  • It's all in what you want I suppose. [`time(2)`](https://linux.die.net/man/2/time) is in seconds, but [`time(3)`](https://linux.die.net/man/3/time) is in milliseconds. I'm old, so I assumed `time(2)`. – Jeffery Thomas Jan 08 '19 at 02:02
9

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())!
   }
}
Manish
  • 608
  • 1
  • 11
  • 23
4

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)!
    }
}
jainashish
  • 4,702
  • 5
  • 37
  • 48
kannan
  • 354
  • 1
  • 7
0

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
3ameration
  • 101
  • 2
  • 3