0

I have tried this in playgrounds (I show in comments what it's printed):

extension NSDate {

    static func currentDate() -> NSDate {
        let calendar = NSCalendar.currentCalendar()
        calendar.timeZone = NSTimeZone.localTimeZone()
        calendar.locale = NSLocale.currentLocale()

        let components = calendar.components([.Year, .Month, .Day], fromDate: NSDate())
        components.hour = 00
        components.minute = 00
        components.second = 00

        return calendar.dateFromComponents(components)! // "Dec 29, 2015, 12:00 AM"
    }
}

print(NSDate.currentDate()) // "2015-12-28 22:00:00 +0000

Anyone has an idea what is going on here ?

I just want the current date (year/month/day). I made this extension because I had problems with NSDate(), it was off by 2 hours (showing 11.24 am, instead of 1.24 pm)

Adrian
  • 19,440
  • 34
  • 112
  • 219
  • you can always use description with locale to print a different time http://stackoverflow.com/a/28405039/2303865 – Leo Dabus Dec 29 '15 at 11:30
  • The idea is that I have some objects added to tableView which also contains a NSDate attribute. When the day changes, lets say is 12.05 am, and I add an element, I want to have the current date which will 30, but with the current code it will still have date day 29 becasue its 2 hours off. I dont just want to print it correctly. – Adrian Dec 29 '15 at 11:35
  • @Adrian: NSDate is an absolute point in time (stored internally as seconds since January 1, 2001 GMT), and knows *nothing* about your time zone etc. To interpret an NSDate according to your time zone, you have to use NSCalendar and NSDateComponents, or convert it to a string with NSDateFormatter. – Martin R Dec 29 '15 at 11:38
  • @MartinR: that's what I tried to use in my code. currentTimezone and currentLocale. Not sure if its the correct way. – Adrian Dec 29 '15 at 11:39
  • @Adrian: But you are converting the date components back to NSDate, and printing an NSDate uses its description method, and that prints the time using a default format in UTC. – Martin R Dec 29 '15 at 11:40
  • @MartinR: Yes because I am retrieving data based on NSDate's. So I am still puzzle, this mean that I will always have incorrect data ? – Adrian Dec 29 '15 at 11:45
  • My question wasn't correct. Maybe I should make a new one, because here I just asked how to print it, but I have a different problem :) – Adrian Dec 29 '15 at 11:46
  • I don't know where your problem is. I assume that you are in the GMT+2 time zone. "2015-12-28 22:00:00 +0000" is *exactly* the same point in time as ""2015-12-29 00:00:00 +0200", i.e. the start of Dec 29 in your time zone. – Martin R Dec 29 '15 at 11:47
  • Do you have time to chat to explain my issue or just post another question ? – Adrian Dec 29 '15 at 11:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/99195/discussion-between-adrian-and-martin-r). – Adrian Dec 29 '15 at 11:48

1 Answers1

1

The NSDate() in Swift and [NSDate date] in Objective-C both holds the UTC date. You can not change it behaviour.

However you can create a formatter and convert to desired locale and date format and store the date as string value.

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = /*your desired format*/
let date = dateFormatter.dateFromString(/*your date string*/)
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • But If I add some elements to table view which have a NSDate attribute at 12.30 am, the date will still be 29 December not 30 December, because of my problem. – Adrian Dec 29 '15 at 11:37