-1

I am getting dates from an api like this:

["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]

but what I want is I want to remove the time from this array of dates to get the dates like this

I want my desired result like this

["2019-07-12", "2019-07-09", "2019-07-09", "2019-07-05", "2019-07-04", "2019-07-02", "2019-07-01"]

The reason why I want because I need to show the events on FSCalendar. How can I do this?

Wings
  • 2,398
  • 23
  • 46
  • Are that all strings? Do they have a fixed format? Can you just take the first 10 characters? – You surely tried something, don't hesitate to show your attempt! – Martin R Jul 12 '19 at 11:24
  • convert it using date formatter – Ravi B Jul 12 '19 at 11:24
  • @MartinR....Yes they all are the strings and the format comes same to all dates and I tried this answer https://stackoverflow.com/questions/35392538/remove-time-from-a-date-like-this-2016-02-10-000000 but t showed the fatal error – Wings Jul 12 '19 at 11:26
  • if all are strings use [split strings](https://stackoverflow.com/questions/25678373/split-a-string-into-an-array-in-swift) and store the first object into array thats all – Anbu.Karthik Jul 12 '19 at 11:26
  • @Anbu.Karthik... I seen that answer but all of them are just splitting a variable of string not array – Wings Jul 12 '19 at 11:28
  • @Wings make sure your strings are local time otherwise (if they are UTC time) you would need to parse your dates – Leo Dabus Jul 13 '19 at 16:11

5 Answers5

4

You can simply use a combination of compactMap(_:) and components(separatedBy:) like,

let arr = ["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]

let result = arr.compactMap({ $0.components(separatedBy: " ").first })
PGDev
  • 23,751
  • 6
  • 34
  • 88
2

Please check below code:

    let arr = ["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]

    var arrOnlyDates = [String]()
    for (_,str) in arr.enumerated(){
        let s = str.components(separatedBy: " ")
        arrOnlyDates.append(s[0])
    }
    print(arrOnlyDates)
Pratik Patel
  • 1,393
  • 12
  • 18
1

You can simply get dates array like this:

let dateArray = ["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]

let onlyDatesArray = dateArray.compactMap({ $0.components(separatedBy: " ")[0] })

print("only dates -> \(onlyDatesArray)")
emrcftci
  • 3,355
  • 3
  • 21
  • 35
1
let dateStrings = ["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]    
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dates = dateStrings.compactMap { dateFormatter.date(from: $0) }
dateFormatter.dateFormat = "yyyy-MM-dd"
let formatedDateStrings = dates.compactMap { dateFormatter.string(from: $0) }
SchrgCat
  • 451
  • 4
  • 5
1

As the date format doesn't change just strip the first 10 characters from each string

let dates = ["2019-07-12 12:43:00", "2019-07-09 12:57:35", "2019-07-09 12:04:33", "2019-07-05 14:32:32", "2019-07-04 17:50:23", "2019-07-02 12:12:30", "2019-07-01 18:09:28"]
let datesWithoutTime = dates.map{ String($0.prefix(10)) }
vadian
  • 274,689
  • 30
  • 353
  • 361