Komal Goyani
Komal Goyani

Reputation: 847

How to get date from static string?

I want to get full date with time like my below code,

func findDate(date: Date)
{
        let format1: DateFormatter = DateFormatter()
        format1.dateFormat = "yyyy-MM-dd"
        let onlyDate = format1.string(from: date) // Here date is like 2019-11-21 

        let dateStr = "\(onlyDate) 10:00 AM"
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm a"
        dateFormatter.locale = Locale(identifier: "en_US_POSIX")
        dateFormatter.calendar = NSCalendar.current
        dateFormatter.timeZone = TimeZone.current

        let newDate = dateFormatter.date(from: dateStr)
        print(newDate) // It returns nil

        dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
        let newDateStrInUTC = dateFormatter.string(from: newDate ?? Date()) // Not proper due to newDate = nil
}

In findDate function date argument has date like 2019-11-21 18:30:00 +0000. I want to add my custom time (10:00 AM) in that date.

I received this date (2019-11-21 18:30:00 +0000) from FSCalender.

I want Output like this 2019-11-21 12:00 PM

Upvotes: 2

Views: 374

Answers (3)

Bhavin Kansagara
Bhavin Kansagara

Reputation: 2916

The problem is with the following line in findDate(date: Date) function.

dateFormatter.dateFormat = "yyyy-MM-dd HH:mm a"

Replace it to following, and your function should start working as expected.

dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"

Explanation:

HH is used for setting date to 24 Hours format.

Use small hh, if you wanted to show date in 12 Hours format with AM/PM

As you are appending 10:00 AM to your onlyDate String. You should use hh:mm a for proper conversion from String to Date.

Hope it helps.

Upvotes: 1

Fahad Masood
Fahad Masood

Reputation: 131

Try this code. It will change time components without changing date.

func findDate(date: Date)
{
    let calendar = Calendar.current
    let timeString = "10:00 AM"
    let formatter = DateFormatter()
    formatter.dateFormat = "hh:mm a"
    let customDate = formatter.date(from: timeString)
    let hour = calendar.component(.hour, from: customDate!)
    let min = calendar.component(.minute, from: customDate!)
    let sec = calendar.component(.second, from: customDate!)
    let resultDate = Calendar.current.date(bySettingHour: hour, minute: min, second: sec, of: date)
    print(resultDate!)
}

Now you can use resultDate according to your requirement. Also take care of those optionals according to your need.

Upvotes: 2

Julian Silvestri
Julian Silvestri

Reputation: 2027

Try this below:

let dateTimeToConvert = yourDateTimeHere
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_EN")
formatter.timeZone = TimeZone.current
formatter.dateFormat = "YYYY-MM-dd HH:mm a"
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"
let converted = formatter.date(from: "\(dateTimeToConvert)")
print(converted)

Note, the locale I have used is different, simply use yours. I have also used a date time to convert with this. But you can achieve what you want by simply adding the time in after date conversion.

You can also mess around with the date time structure like so

format = "HH:mm: a 'on' MMMM dd, yyyy"

this format when used will like like this

"9:01 am on January 12, 2019"

Upvotes: 1

Related Questions