0

I want to get the only date in iOS and not time my code is

extension Date{
   var DateInDate: Date{
      let formatter = DateFormatter()
      formatter.setLocalizedDateFormatFromTemplate("yyyy-MM-dd")
      formatter.locale = Locale(identifier: "en_IN")
      let dateInString = formatter.string(from: self)
      return formatter.date(from: dateInString)!
   }
}

if I am doing by the above format I am getting the answer as "Apr 3, 2019 at 12: 00 AM"

my other code is

extension Date{
   var DateInDate: String{
      let formatter = DateFormatter()
      formatter.dateStyle = .short
      formatter.timeStyle = .none
      formatter.setLocalizedDateFormatFromTemplate("yyyy-MM-dd")
      formatter.locale = Locale(identifier: "en_IN")
      let dateInString = formatter.string(from: self)
  }
}

but in this way, I am getting in string format and not a Date format

I want the answer in date format

5
  • 6
    A Date object represents a specific point in time. So you cannot just discard the hour/minute/second part of it. What is it that you are actually trying to achieve? Apr 3, 2019 at 9:04
  • Time is a part of date object you can not remove it but you can make it 00:00 if needed, but before doing that you should verify that you're doing the thing you actually want to do. Apr 3, 2019 at 9:11
  • lets say today is april 3rd 2019 based on my location i want just that only not the time like april 3rd 2019 16:00:43 as my app is storing data of particular day not time Apr 3, 2019 at 9:11
  • I think you don't need to return a Date instead return a String from formatter.string(from: self).
    – Kamran
    Apr 3, 2019 at 9:13
  • in the first code i am returning the date only it still has time in it Apr 3, 2019 at 9:53

3 Answers 3

1

As mentioned in the comments a Date instance without the time portion is impossible.

In terms of Date a timeless date is midnight, the start of the day.

There is an convenience API in Calendar:

let startOfDay = Calendar.current.startOfDay(for: Date())
3
  • I am still getting the time as 12:00 AM in the answer Apr 3, 2019 at 10:39
  • Yes of course because the time is 00:00:00 (mostly) and printing a date prints a generic description string which includes the time.
    – vadian
    Apr 3, 2019 at 10:42
  • I really want to upvote not because it provided the answer I wanted but decreases the 5 line of code to just 1 Apr 3, 2019 at 11:21
0

You should use DateComponents instead. Date would always have the time.

4
  • Explain your answer with example how to get only Date from datecomponents otherwise this answer is low quality and containing link only and is to be a comment. Moreover please go though this how-to-answer
    – dahiya_boy
    Apr 3, 2019 at 9:24
  • 1
    let calendar = NSCalendar.current let comps = calendar.dateComponents([.year, .month, .day], from: Date()) You can`t get Date object and should work only with this.
    – vlad_sh
    Apr 3, 2019 at 9:37
  • Update your answer instead of posting comments to it by clicking edit Apr 3, 2019 at 9:40
  • I am able to get only year month and day using Vlad_sh's answer but if I convert this back to date format (as my model has date type and not date component type) I am getting current time along with the year month and day Apr 3, 2019 at 11:12
0

Try this:

 let date = Date()
 let dateFormatter = DateFormatter()
 dateFormatter.timeStyle = DateFormatter.Style.none
 dateFormatter.dateStyle = DateFormatter.Style.short
 dateFormatter.string(from: date)
1
  • it will return an string in my model i am using date not string Apr 3, 2019 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.