Swift : how to convert String to string with time format

convert string to date swift
swiftui date to string
swift dateformatter locale
string to date format in ios swift
swift print date in format
convert string into datetime swift
swift get current date and time as string

I have string values include time with formatted "HH:mm" (16:50)

i have to convert this value to "hh:mm" format (04:50) as a string in SWIFT

i have tried NSDateFormatter like

let time_format = NSDateFormatter() 
time_format.dateFormat = "hh:mm"
var date_from_str = time_format.dateFromString("my string time value")
var str_from_date = time_format.stringFromDate (date_from_str) 

but its not working.. Please tell any solution.

You need 2 NSDateFormatters, one for dateFromString, one for stringFromDate

let inFormatter = NSDateFormatter()
inFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
inFormatter.dateFormat = "HH:mm"

let outFormatter = NSDateFormatter()
outFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
outFormatter.dateFormat = "hh:mm"

let inStr = "16:50"
let date = inFormatter.dateFromString(inStr)!
let outStr = outFormatter.stringFromDate(date)
println(outStr) // -> outputs 04:50

Swift : how to convert String to string with time format, You need 2 NSDateFormatter s, one for dateFromString , one for stringFromDate let inFormatter = NSDateFormatter() inFormatter.locale� The format must match with string date time or it throws FormatException. So to overcome this issue you can use string format array which has some possibilities. Suppose your date may be in format like “12/12/2015” or “12-12-2015”, here you need to pass string array with format like “MM/dd/yyyy” and “MM-dd-yyyy”.

2020 | SWIFT 5.1:

let inFormatter = DateFormatter()
inFormatter.locale = Locale(identifier: "en_US_POSIX")
inFormatter.dateFormat = "HH:mm"

let outFormatter = DateFormatter()
outFormatter.locale = Locale(identifier: "en_US_POSIX")
outFormatter.dateFormat = "hh:mm"

let inStr = "16:50"
let date = inFormatter.date(from: inStr)!
let outStr = outFormatter.string(from: date)
println(outStr) // -> outputs 04:50

Convert time string into date swift, Always remember this: Date / NSDate stores times in UTC. If your timezone is anything but UTC, the value returned by print(date) will always be� datetime is an expression that evaluates to date or datetime value that you want to convert to a string; sytle specifies the format of the date. The value of style is a number predefined by SQL Server. The style parameter is optional. The following table illustrates the valid style and the corresponding format of the datetime after converting

I believe you should write the time inside the string.

    let dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "hh:mm"
    var date = dateFormatter.dateFromString("00:00")
    var str_from_date = dateFormatter.stringFromDate (date)

How to convert dates and times to a string using , Converting a Date instance to a string using one of the built-in time formats. Converting a Date instance to a string using a completely custom� Date Time Formatting in Swift is based off of the DateFormatter class which can be used to manipulate dates and times. An Instance of DateFormatter creates a string representation of NSDate objects, and can also convert textual representations of dates and times into NSDate objects.

This is an old post, but I think it's worth noting that isLenient allows you to use 1 formatter instead of 2.

func normalizeTimeString(_ original: String) -> String? {
    let formatter = DateFormatter()
    formatter.isLenient = true
    formatter.dateFormat = "hh:mm"

    guard let date = formatter.date(from: original) else { return nil }
    let corrected = formatter.string(from: date)
    return corrected

normalizeTimeString("16:50") // Returns "04:50"

How To Format Date Time In Swift, An Instance of DateFormatter creates a string representation of NSDate objects, and can also convert textual representations of dates and times into NSDate� Strings and Characters¶ A string is a series of characters, such as "hello, world" or "albatross". Swift strings are represented by the String type. The contents of a String can be accessed in various ways, including as a collection of Character values. Swift’s String and Character types provide a fast, Unicode-compliant way to work with

Parsing and Formatting Dates in Swift, Working with dates is a task that is universally applicable to Swift developers. Working with Date and Time - a fantastic blog post covering tons of useful take a raw value (say a String or Int ) and convert it into a Date instance we dateFormat = "MM/dd/yyyy" dateLabel.text = formatter.string(from: date). How to generate a date time stamp, using the format standards for ISO 8601 and RFC 3339? The goal is a string that looks like this: "2015-01-01T00:00:00.000Z" Format: year, month, day, as "XXXX

How to work with dates and times in Swift 5, part 2: Formatting and , We use this class to convert Date s into formatted String s that match print(" Swift's debut time: \(myFormatter.string(from: swiftUIDebutDate))."). Just like with CONVERT() the string needs to be in a style that’s able to be converted to the specified data type, otherwise you’ll get an error: SELECT CAST('Homer' AS date) AS Result; Result: Conversion failed when converting date and/or time from character string. Example 2 – Convert to datetime Data Type

Dates and DateFormatter in Swift – LearnAppMaking, In this tutorial, we discuss how you can convert date/time to strings, formatting, how to calculate date durations, DateComponents, locales, and� I'm trying to call a function one argument is current time and Date and other is a file name, They both are strings. How can I convert Date() to a string. I tried: writeToFile(content: Date(), fileName: "jm.txt") but it gave me error: Cannot convert value of type 'Date' to expected argument type 'String'

  • Just a wild guess.... shouldn't it be var date_from_str = time_format.dateFromString("16:50") ?
  • why my question get downvote? and first answer erased from page while thats true.. what is going on here
  • I don't mean to sound rude, but did you look at the stackoverflow.com/tour ?