I need to convert "2022-01-20T00:00:00.000Z" to "dd MMM yyy" format.
I have tried doing it as suggested in a stackoverflow answer but it returns nil
func convertDate(date:String)-> String{
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "dd MMM yyy"
if let date = dateFormatterGet.date(from: String(date)) {
let convertedDate = dateFormatterPrint.string(from: date)
return convertedDate
} else {
print("There was an error decoding the string")
}
return "nil"
}
The fractional seconds are missing in the date format string. It's yyyy-MM-dd'T'HH:mm:ss.SSSZ.
And you are not using the date parameter. To avoid confusion with the local variable date rename it.
And you don't need two date formatters and it's highly recommended to set the Locale to a fixed value
func convertDate(dateString: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
if let date = dateFormatter.date(from: dateString) {
dateFormatter.dateFormat = "dd MMM yyy"
return dateFormatter.string(from: date)
} else {
print("There was an error decoding the string")
return ""
}
}
got the answer
func convertDate(date:String)-> String{
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.sssZ"
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "dd MMM yyy"
if let date = dateFormatterGet.date(from: String(date)) {
let convertedDate = dateFormatterPrint.string(from: date)
return convertedDate
} else {
print("There was an error decoding the string")
}
return "nil"
}
print(convertDate(date: "2022-01-20T00:00:00.000Z"))
Related
I'm trying to parse the following String input "2020-04-05 19:02:02" into Date, which I'm using the following code to do that:
static func getDateFromString(_ dateStr: String) -> Date? {
let dateFormater = DateFormatter()
dateFormater.dateFormat = "yyyy-MM-dd H:mm:ss"
dateFormater.timeZone = TimeZone(identifier: "UTC")
return dateFormater.date(from: dateStr)
}
The problem is that not working, it returns nil
Any suggestion? Thank you
Here you go, this works fine:
In dateString pass data in string format and for dateFormat pass format you want and to choose format use NSDateFormatter:
let expiryDateString = "2020-04-05 19:02:02"
let dateformat = "yyyy-MM-dd HH:mm:ss"
let expireDate = DateHelper.getDateFrom(expireDateString, dateformat)
static func getDateFrom(dateString: String,dateFormat: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
dateFormatter.timeZone = .current
guard let date = dateFormatter.date(from: dateString) else {return nil}
return date
}
I am trying to changing the date format from "MMM dd, yyyy" to "yyyy-MM-dd h:mm:ss" by using below formattedDateFromString function. Here, Time I am not sending during the format change but by default in result after format change I am getting 12:00:00. I need to set it is 11:00:00.
let result = formattedDateFromString(dateString: "Sep 20, 2019"), withFormat: "yyyy-MM-dd h:mm:ss")
func formattedDateFromString(dateString: String, withFormat format: String) -> String? {
let inputFormatter = DateFormatter()
inputFormatter.dateFormat = "MMM dd, yyyy"
if let date = inputFormatter.date(from: dateString) {
print(date)
let outputFormatter = DateFormatter()
outputFormatter.dateFormat = format
return outputFormatter.string(from: date)
} else {
print("nope")
}
return nil
}
A solution is to add 11 Hours hard-coded.
And one date formatter is sufficient
func formattedDateFromString(dateString: String, withFormat format: String) -> String? {
let formatter = DateFormatter()
formatter.dateFormat = "MMM dd, yyyy HH"
guard let date = formatter.date(from: dateString + " 11") else { print("nope"); return nil }
formatter.dateFormat = format
return formatter.string(from: date)
}
I am trying to change date time concatenation and changing string input format to another format. I tried below code. Here, I can able to change the date format but need to concatenate current time before passing input date string.
My Code Below
override func viewDidLoad() {
super.viewDidLoad()
let result = formattedDateFromString(dateString: "28 Aug, 2019", withFormat: "dd-MM-yyyy")
self.resultLabel.text = result
}
func formattedDateFromString(dateString: String, withFormat format: String) -> String? {
let inputFormatter = DateFormatter()
inputFormatter.dateFormat = "dd/MM/yyyy"
if let date = inputFormatter.date(from: dateString) {
let outputFormatter = DateFormatter()
outputFormatter.dateFormat = format
return outputFormatter.string(from: date)
}
return nil
}
Expected output like :
28-08-2019 5:00 PM
func formattedDateFromString(dateString: String, withFormat format: String) -> String? {
let inputFormatter = DateFormatter()
inputFormatter.dateFormat = "dd MMM, yyyy"
if let date = inputFormatter.date(from: dateString) {
print(date)
let outputFormatter = DateFormatter()
outputFormatter.dateFormat = format
return outputFormatter.string(from: date)
} else {
print("nope")
}
return nil
}
let result = formattedDateFromString(dateString: "28 Aug, 2019", withFormat: "dd-MM-yyyy - h:mm a")
print(result)
Outputs:
"28-08-2019 - 12:00 AM"
In general I found this website to be really useful when trying to reason about dates in Swift/Objective-C
You can use this function
func convDate(dateToConvert: String) -> String? {
//Input Format
let df = DateFormatter()
df.dateFormat = "dd LL, YYYY"
//Convert input date string to date
guard let date = df.date(from: dateToConvert) else { print("Incorrect date format"); return nil }
//Output format
let outDF = DateFormatter()
outDF.dateFormat = "dd-MM-YYYY"
let hourDF = DateFormatter()
hourDF.dateFormat = "h:mm a"
hourDF.amSymbol = "AM"
hourDF.pmSymbol = "PM"
//format to string dd-MM-YYYY H:mm a
return outDF.string(from: date) + " " + hourDF.string(from: Date())
}
example
print(convDate(dateToConvert: "13 Aug, 2019"))
This question already has an answer here:
NSDateFormatter dateFromString returns date with wrong month
(1 answer)
Closed 4 years ago.
I am convert date from "yyyy-mm-dd'T'HH:mm:ss.SSSZ" to "dd-mm-yyyy" and my date is "2018-09-11T09:53:25.000+0000" and this is my code
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-mm-dd'T'HH:mm:ss.SSSZ"
let dateFromInputString = dateFormatter.date(from: "2018-09-11T09:53:25.000+0000")
dateFormatter.dateFormat = "dd-mm-yyyy"
if(dateFromInputString != nil){
return dateFormatter.string(from: dateFromInputString!)
}
else{
debugPrint("could not convert date")
return "N/A"
}
but i am getting converted date 11-23-2018 like this
But my aspect is 11-09-2018
Your dateFormat is wrong. The ambiguity in your format should have been clear when you used 'mm' for both months and minutes.
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
mm - minutes
MM - months
The same goes for the second date format too.
Refer this for more information on date formats.
Try this
func GetFormatedDate(date_string:String,dateFormat:String)-> String{
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = dateFormat
let dateFromInputString = dateFormatter.date(from: date_string)
dateFormatter.dateFormat = "dd-MM-yyyy" // Here you can use any dateformate for output date
if(dateFromInputString != nil){
return dateFormatter.string(from: dateFromInputString!)
}
else{
debugPrint("could not convert date")
return "N/A"
}
}
Use Like this :
self.GetFormatedDate(date_string: "2018-09-11T09:53:25.000+0000", dateFormat: "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
Output :
11-09-2018
should be
dateFormatter.dateFormat = "dd-MM-yyyy"
Try it
Your dateFormater is wrong it should be yyyy-MM-dd'T'HH:mm:ss.SSSZ not this yyyy-mm-dd'T'HH:mm:ss.SSSZ
dateFormatter.dateFormat = "dd-mm-yyyy"
to
dateFormatter.dateFormat = "dd-MM-yyyy"
Try this:
extension String {
func convertDate() -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
if let dateInLocal = dateFormatter.date(from: self) {
dateFormatter.dateFormat = "dd-MM-yyyy"
return dateFormatter.string(from: dateInLocal)
}
return "NA"
}
}
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let dateFromInputString = dateFormatter.date(from: "2018-09-11T09:53:25.000+0000")
dateFormatter.dateFormat = "dd-MM-yyyy"
if(dateFromInputString != nil){
return dateFormatter.string(from: dateFromInputString!)
}
else{
debugPrint("could not convert date")
return "N/A"
}
I want to get my Date in DD.MM.YYYY HH:mm:ss as a String.
I use the following extension:
extension Date {
var localTime: String {
return description(with: Locale.current)
}
}
and the following code when my datePicker changes:
#IBAction func datePickerChanged(_ sender: UIDatePicker) {
dateLabel.text = datePicker.date.localTime
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy hh:mm"
let TestDateTime = formatter.date(from: datePicker.date.localTime)
}
What am I doing wrong?
Your code is completely wrong. Just do the following:
#IBAction func datePickerChanged(_ sender: UIDatePicker) {
let formatter = DateFormatter()
formatter.dateFormat = "dd.MM.yyyy HH:mm"
dateLabel.text = formatter.string(from: sender.date)
}
This will convert the date picker's chosen date to a string in the format dd.MM.yyyy HH:mm in local time.
Never use the description method to convert any object to a user presented value.
Just used the function in your code(swift 4.2).
public func convertDateFormatter(date: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"//this your string date format
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
dateFormatter.locale = Locale(identifier: "your_loc_id")
let convertedDate = dateFormatter.date(from: date)
guard dateFormatter.date(from: date) != nil else {
assert(false, "no date from string")
return ""
}
dateFormatter.dateFormat = "HH:mm a"///this is what you want to convert format
dateFormatter.timeZone = NSTimeZone(name: "UTC") as TimeZone!
let timeStamp = dateFormatter.string(from: convertedDate!)
print(timeStamp)
return timeStamp
}
Thanks