I try to format the time in French in my iOS app written in Swift 3
Here the data string
2000-01-01T14:00:00.000Z
Here method swift 3
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = "hh:mm"
return dateFormatter.string(from: timeStart!);
}
On my application gives its 2:00
And I want to display 2:00 p.m.
I used it: dateFormatter.locale = Locale (identifier: "fr_FR")
but its not working there have another manipulation do?
You need to fix both date formats:
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = "hh:mm a"
return dateFormatter.string(from: timeStart!);
}
Better yet, don't use dateFormat to convert the Date to a String. Instead, set the timeStyle so it's formatted properly without the need to force a specific format.
func get_time_start() -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let timeStart = dateFormatter.date(from: time_start)
dateFormatter.dateFormat = nil
dateFormatter.timeStyle = .medium
dateFormatter.dateStyle = .none
return dateFormatter.string(from: timeStart!);
}
In either case, only set the locale if you want all users to see the time formatted in that specific locale (and that would be rare).
Related
I cannot seem to figure out why only "24:00" returns nil instead of 12:00AM, whereas "23:59" returns correctly as 11:59PM.
let dateAsString = "24:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale
let date = dateFormatter.date(from: dateAsString)
dateFormatter.dateFormat = "h:mm a"
let Date12 = dateFormatter.string(from: date!)
print(Date12)
You are using the wrong date format for the hour component. For values from 1-24 you have to use k, if you have a leading zero you need to use kk:
let dateAsString = "24:00"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "kk:mm"
if let date = dateFormatter.date(from: dateAsString) {
dateFormatter.dateFormat = "h:mm a"
let date2 = dateFormatter.string(from: date)
print(date2) // "12:00 AM\n"
}
I have assigned the value of date to a dictionary like so...
userDetailsDictionary["birth_date"] = "\(birthDateVar)"
Now this gives the date in dd-mm-yyyy. But I want the date in yyyy-mm-dd. For that I tried something like this...
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let bday = dateFormatter.date(from: birthDateVar)
dateFormatter.dateFormat = "yyyy-MM-dd"
let bdayString = dateFormatter.string(from: bday!)
Though birthDateVar has the value in it, while I'm trying to convert it, it is showing as nil and so bday is becoming nil. Confused as to what the reason is...
change your date format from
dateFormatter.dateFormat = "MM-dd-yyyy"
to
dateFormatter.dateFormat = "dd-MM-yyyy"
Updated answer
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
let bday = dateFormatter.date(from: birthDateVar)
dateFormatter.dateFormat = "yyyy-MM-dd"
let bdayString = dateFormatter.string(from: bday!)
I'm trying to convert string to date, and again date to string.
I tried below, but it gives me a nil value error
let string = "2017-07-11T06:52:15.948Z"
let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: string)!
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")
then I tried to use only the date without time by seperating the exact string like below
let string = "2017-07-11T06:52:15.948Z"
let dateonly = (string.components(separatedBy: "T"))[0]
let dateformatter = DateFormatter()
dateformatter.dateFormat = "yyyy-mm-dd"
dateformatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") as Locale!
let date = dateformatter.date(from: dateonly!)
print(date ?? "no date ")
dateformatter.dateFormat = "MMM d, yyyy" //Your New Date format as per requirement change it own
let newDate = dateformatter.string(from: date!) //pass Date here
print(newDate)
this works fine. but show me the wrong month
hope your help with this
for reference purpose I taken the answer from here for append the milliseconds SSS
just change the date format
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
to
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
for full code
let string = "2017-07-11T06:52:15.948Z"
let dateFormatter = DateFormatter()
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from: string)!
dateFormatter.dateFormat = "MMM d, yyyy" ; //"dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale --> but no need here
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")
output
let dateFormatter = DateFormatter()
let dateFormatter2 = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: event!)
dateFormatter2.timeZone = TimeZone.current
let dateString = dateFormatter2.string(from: date!)
let finalDate = dateFormatter2.date(from: dateString)
return finalDate!
I have sports events that give me string dates in UTC which I am trying to convert into local time. This code is similar to the dozens of examples given in this site when asked this question, yet it doesn't work.
This is the iso8601 formatter :
static let iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
return formatter
}()
This turns your string into a Date.
You will need to add the time difference, most likely through TimeZone.autoupdatingCurrent
I have a date string in this format:
2016-06-20T13:01:46.457+02:00
and I need to change it to something like this:
20/06/2016
Previously I have always used this library -> SwiftDate to manipulate the dates, but it doesn't work now.
I tried also something like:
let myDate = self.dateNoteDict[indexPath.row]!
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss.SSSSxxx"
let date = dateFormatter.dateFromString(myDate)
print("date -> \(date)")
but it doesn't work. How can I do?
Thanks in advance.
The standard ISO8601 date format with fractional seconds and time zone is
yyyy-MM-dd'T'HH:mm:ss.SSSZ
let myDate = "2016-06-20T13:01:46.457+02:00"
let dateFormatter = NSDateFormatter()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") // edited
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.dateFromString(myDate)!
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.stringFromDate(date)
Swift 3:
let myDate = "2016-06-20T13:01:46.457+02:00"
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // edited
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from:myDate)!
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.string(from:date)
In macOS 10.13+, iOS 11+ ISO8601DateFormatter is an alternative as input formatter
let myDate = "2016-06-20T13:01:46.457+02:00"
let inputFormatter = ISO8601DateFormatter()
inputFormatter.formatOptions = [.withFullDate, .withFullTime, .withFractionalSeconds]
let date = dateFormatter.date(from:myDate)!
let outputFormatter = DateFormatter()
outputFormatter.locale = Locale(identifier: "en_US_POSIX")
outputFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.string(from:date)
I always use this code while converting String to Date .
(Swift 3)
extension String
{
func toDate( dateFormat format : String) -> Date
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
if let date = dateFormatter.date(from: self)
{
return date
}
print("Invalid arguments ! Returning Current Date . ")
return Date()
}
}
and just call like . .
print ( "2016-06-20T13:01:46.457+02:00".toDate(dateFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ") )
//Capital HH for 24-Hour Time
I always use this code while converting String to Date. (Swift 4.2)
let myDate = datePicker.date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = myDate
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.string(from:date)
print(dateString)
You can try manually also:
let myDate = "2019-02-22T08:21:11+0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = myDate
dateFormatter.dateFormat = "dd/MM/yyyy"
let dateString = dateFormatter.string(from:date)
print(dateString)