0

I am trying to remove time from date using NSDateFormatter. This is the code:

func dateWithOutTime( datDate: NSDate?) -> NSDate {

    let formatter = NSDateFormatter()
    formatter.dateFormat = "dd-MM-yyyy"
    let stringDate: String = formatter.stringFromDate(datDate!)
    let dateFromString = formatter.dateFromString(stringDate)

return dateFromString!
}

If i send in ex 04-01-2016 12:00:00, the return is 03-01-2016 23:00:00 I have tried changing the dateFormat, but it still keeps to subtracting a day from the date... Why? Please Help :)

Vegar Flo
  • 61
  • 5

2 Answers2

7

The easiest way is to use startOfDayForDate of NSCalendar

Swift 2:

func dateWithOutTime( datDate: NSDate) -> NSDate {
    return NSCalendar.currentCalendar().startOfDayForDate(datDate)
}

Swift 3+:

func dateWithOutTime(datDate: Date) -> Date {
    return Calendar.current.startOfDay(for: datDate)
}

or to adjust the time zone to UTC/GMT

Swift 2:

func dateWithOutTime( datDate: NSDate) -> NSDate {
  let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
  calendar.timeZone = NSTimeZone(forSecondsFromGMT: 0)
  return calendar.startOfDayForDate(datDate)
}

Swift 3+:

func dateWithOutTime(datDate: Date) -> Date {
  var calendar = Calendar(identifier: .gregorian)
  calendar.timeZone = TimeZone(secondsFromGMT: 0)!
  return calendar.startOfDay(for: datDate)
}
vadian
  • 274,689
  • 30
  • 353
  • 361
1

Two things:

(a) This can be done without using NSDateFormatter. (b) Calling print(aDate) will give you the UTC time, not in your local time. After losing too many brain cells trying to mentally convert back and forth, I decided to make an extension to NSDate to print it in my local timezone.

// NSDateFormatter is expensive to create. Create it once and reuse
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZZ"

extension NSDate {
    // This will print the date in the local timezone
    var localTime: String {
        return dateFormatter.stringFromDate(self)
    }
}

func dateWithOutTime(datDate: NSDate?) -> NSDate {
    let calendar = NSCalendar.currentCalendar()
    let components = calendar.components([.Year, .Month, .Day], fromDate: datDate!)
    return calendar.dateFromComponents(components)!
}

let datDate = NSCalendar.currentCalendar().dateWithEra(1, year: 2016, month: 1, day: 4, hour: 12, minute: 0, second: 0, nanosecond: 0)
let result = dateWithOutTime(datDate)
print(result.localTime)
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • Thank you! What you say about calling print explains my trouble. Live in Norway and are 1 hour in front of UTC. Does this mean that on my phone the date would be "correct" and computer shows "wrong"? – Vegar Flo Jan 04 '16 at 14:07
  • `23:00 UTC = 00:00 CET`, they both represent the same moment in time. `print` always show the UTC so unless you live in the British Isles, it will give you "wrong" output every time. – Code Different Jan 04 '16 at 14:15
  • You are using date formatter and stating that it can be done without it – Leo Dabus Jan 04 '16 at 14:20
  • Btw date formatter default it is local time so you don't need to specify it again – Leo Dabus Jan 04 '16 at 14:33