remove time from a date like this 2016-02-10 00:00:00

21,258

Solution 1

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)

Solution 2

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
}

Solution 3

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

Solution 4

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"

Solution 5

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.

Share:
21,258
hellosheikh
Author by

hellosheikh

Updated on June 11, 2021

Comments

  • hellosheikh
    hellosheikh almost 3 years

    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
    
  • Frank
    Frank over 3 years
    Thanx exactly what i needed in conjunction to check if a date is between two dates without the time
  • nKognito
    nKognito almost 3 years
    Might be worth to mention, that Calendar's timeZone property should set appropriately