12

hello I have a date like this

2016-02-10 00:00:00

I want to get only date from it in this style

14.05.2016 or 14-05-2016

This is what I have tried

let dateFormatter = NSDateFormatter()
let date = "2016-02-10 00:00:00"
dateFormatter.dateFormat = "dd-MM-yyyy"
let newdate = dateFormatter.dateFromString(date)
print(newdate) //nil is coming
Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63
hellosheikh
  • 2,929
  • 8
  • 49
  • 115

10 Answers10

42

A better way than proposed versions is not to convert from date using a string formatter, but instead using calendar:

public func removeTimeStamp(fromDate: Date) -> Date {
    guard let date = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: fromDate)) else {
        fatalError("Failed to strip time from Date object")
    }
    return date
}
Jeremie D
  • 4,145
  • 1
  • 35
  • 41
15

Using an extension on the Date:

extension Date {
    public var removeTimeStamp : Date? {
       guard let date = Calendar.current.date(from: Calendar.current.dateComponents([.year, .month, .day], from: self)) else {
        return nil
       }
       return date
   }
}

Usage:

let now = Date()
let nowWithouTime = now.removeTimeStamp
Umair
  • 1,203
  • 13
  • 15
8

okay I solved this myself

  let date = "2016-02-10 00:00:00"
  let dateFormatter = NSDateFormatter()

  dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
  let dateFromString : NSDate = dateFormatter.dateFromString(date)!
  dateFormatter.dateFormat = "dd-MM-yyyy"
  let datenew= dateFormatter.stringFromDate(dateFromString)

    print(datenew)
hellosheikh
  • 2,929
  • 8
  • 49
  • 115
4

Swift 5.5+

Use formatted(_:)

let now = Date.now
let date = now.formatted(.iso8601.year().month().day().dateSeparator(.dash))

Or formatted(date:time:)

let now = Date.now
let date = now.formatted(date: .abbreviated, time: .omitted)

Instead of .abbreviated, you may use a DateStyle such as .long, or .numeric.

https://developer.apple.com

mahan
  • 12,366
  • 5
  • 48
  • 83
2

Your code is not correct.

If you have NSDate instance that you want to convert to String using NSDateFormatter

You use this code:

let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateString = dateFormatter.stringFromDate(date)

The problem in your code is that you have a date string with value 2016-02-10 00:00:00 but you parse it using date format `dd-MM-yyyy' this is why you get a nil Date.

Instead you need to parse it first using dateFormatter.dateFormat = "yyy-MM-dd hh:mm:ss"

Ahmad Baraka
  • 1,093
  • 9
  • 12
1

If you have an input date string (or rather, date-and-time string) in one format and you want to output in a different format then you need 2 date formatters: An input formatter that takes the source string format and converts it to an NSDate (using dateFromString) and then an output formatter that takes the NSDate and converts it to your output date string (using stringFromDate).

Your code is wrong because you are creating a date formatter configured for your output date string format and trying to use it to convert your input date string to an NSDate.

I am not an expert on NSDateFormatter date strings. Any time I need to work with them I have to dig out the docs and figure out the solution to the specific problem I'm trying to solve. Thus I'm going to leave that part of the problem to you. Suffice it to say that you'll need an input date formatter that uses a format string that exactly matches the format of your input date string. This can be tricky because if it isn't exactly correct it simply fails and returns a nil NSDate. The output date formatter is easier because if it isn't quite right, your output date will not look the way you want it to look but that will be obvious.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
1

Recommend you to use library SwiftDate with dozen of handy options. For your case use date truncating e.g.:

let date = "2017-07-22 15:03:50".toDate("yyyy-MM-dd HH:mm:ss", region: rome)
let truncatedTime = date.dateTruncated(from: .hour) // 2017-07-22T00:00:00+02:00
Asike
  • 309
  • 5
  • 14
0

For Swift 4 and above, You can go with the following code

 let date = "2016-02-10 00:00:00"
 let dateFormatter = DateFormatter()
 let date = modelDeals.dealItemInDetail?.validTo ?? ""
 dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
 let dateFromString : NSDate = dateFormatter.date(from: date)! as NSDate
 dateFormatter.dateFormat = "dd-MM-yyyy"
 let datenew = dateFormatter.string(from: dateFromString as Date)
Varun P V
  • 1,092
  • 1
  • 12
  • 30
-1

Swift 3 Version:

let date = "2016-02-10"
let dateformater = DateFormatter()
    dateformater.dateFormat = "YYYY-MM-dd"
    let dateString = dateformater.date(from: date)

    //print date
    print(dateString)
Kegham K.
  • 1,589
  • 22
  • 40
-1

For : (Swift 3) applications,

if you are using Date() objects make sure you set timeStyle of DateFormatter() to none

Example:

let today = Date()
let dateFormatter = DateFormatter()
//dateFormatter.dateFormat = "yyyy-MM-dd", upto you
dateFormatter.dateFormat = "dd-MM-yyyy"
//This is Important!
dateFormatter.timeStyle = DateTimeFormatter.Style.none
let dateString = dateFormatter.string(from: today)
computingfreak
  • 4,939
  • 1
  • 34
  • 51