0

I am current at timezone UTC-05:00. When I call the function NSDate(timeIntervalSince1970:0), it returns back "Dec 31, 1969, 7:00 PM"

let date = NSDate.init(timeIntervalSince1970: 0) // "Dec 31, 1969, 7:00 PM"
print(date) // "1970-01-01 00:00:00 +0000\n"

I read about this How to get NSDate day, month and year in integer format? But the problem is that with the following, I always get 1969-12-31 because of the 5 hour time difference.

let calendar = NSCalendar.currentCalendar()
calendar.getEra(&era, year:&year, month:&month, day:&day, fromDate: date)
year   // 1969
month  // 12
day    // 31

var hour = 0, minute = 0, second = 0
calendar.getHour(&hour, minute: &minute, second: &second, nanosecond: nil, fromDate: date)
hour        // 19
minute      // 0
second      // 0

Is there a way to get the current year, month, day values and etc. in the current timezone. What I am looking for here is:

year   // 1970
month  // 01
day    // 01
Community
  • 1
  • 1
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • `NSTimeZone systemTimeZone` – zc246 Feb 26 '16 at 17:05
  • 1
    With `timeIntervalSince1970` of 0, that is the correct information for your local time zone. – Tom Harrington Feb 26 '16 at 17:06
  • 1
    The problem here is that you are asking for a date that has being is fixed to UTC 0. You are going to always get that result unless you change the timezone for this calendar to UTC 0. – Hugo Alonso Feb 26 '16 at 17:06
  • Thanks for point this out. I understand that I can get the current system time zone with `systemTimeZone` and I can adjust the year, month and etc. However, I hope that there is a more elegant way to do it. – Yuchen Feb 26 '16 at 17:08
  • 1
    There is nothing to adjust. `NSDate(timeIntervalSince1970: 0)` is the *defined* as the start of the "epoch" Jan 1, 1970, UTC (see https://en.wikipedia.org/wiki/Unix_time). That *is* "Dec 31, 1969, 7:00 PM" in your timezone. – Martin R Feb 26 '16 at 17:16
  • What are you trying to achieve? Usually there is no work necessary at all. – Eiko Feb 26 '16 at 17:16

2 Answers2

2

The timeIntervalSince1970 initializer gives you (as documented) an NSDate which is some number of seconds since Jan 1 1970 at 00:00:00 in UTC, not in your local time zone. Those results you're getting are correct, because they're showing your local time zone's offset from that time. You're passing in 0, so you're getting Jan 1 1970 at 00:00:00 UTC, and then NSCalendar is giving you the equivalent date and time in your local time zone.

If you want to get Jan 1 1970 at 00:00:00 in your local time zone, you need to request that date specifically from NSCalendar:

let calendar = NSCalendar.currentCalendar()
calendar.timeZone = NSTimeZone.localTimeZone()

let date = calendar.dateWithEra(1, year: 1970, month: 1, day: 1, hour: 0, minute: 0, second: 0, nanosecond: 0)
calendar.getEra(&era, year:&year, month:&month, day:&day, fromDate: date!)
year   // 1970
month  // 1
day    // 1

This is not a 0 offset for timeIntervalSince1970. If you check, you'll see that the result corresponds to your time zone's offset from UTC:

date?.timeIntervalSince1970 // 25200, for me
Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
1

This will return the current date ex. 02/26/2016

// Format date so we may read it normally
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd/M/yyyy"
let currentDate = String(dateFormatter.stringFromDate(NSDate()))

Swift 3.0

NSDateFormatter > DateFormatter && NSDate > Date

let df = DateFormatter()

df.timeZone   = .current
df.dateStyle  = .medium
df.timeStyle  = .short
df.dateFormat = "dd/M/yyyy"

let currentDate = df.string(from: Date())
ZZZZtop
  • 457
  • 1
  • 5
  • 19