This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 5 years ago.
I have a String like "20170713 111326" and it should be changed to "13/07/2017" and "11:13 AM".What'll be the easiest way?
Swift 3.0
Try this code
let st = "20170713 111326"
let dateFmt = DateFormatter()
dateFmt.dateFormat = "yyyyMMdd HHmmss"
if let date = dateFmt.date(from: st) {
dateFmt.dateFormat = "dd/MM/yyyy HH:mm a"
let finalDate = dateFmt.string(from: date))
}
Related
This question already has answers here:
Convert string to date in Swift
(18 answers)
Closed 1 year ago.
var str = "2021-05-23T06:35:47.409Z"
var formatter = DateFormatter()
formatter.dateFormat = "MMM d yyyy, h:mm:ss a"
let formattedtoDate = formatter.date(from: str)
let formattedtoString = formatter.string(from: formattedtoDate) //Error Cannot force unwrap value of non-optional type 'String'
cell.date_announce.text = formattedtoString
I'm trying to format sting to Date() and format Date to String in order to set value to date_announce label. Can anyone help me please?
If you break down what you are trying to do, there are actually 2 steps that require two different date formatters.
Convert an input date string (e.g. "2021-05-23T06:35:47.409Z") to a Date
Convert the Date to an output string in a different format.
Try this code instead:
var str = "2021-05-23T06:35:47.409Z"
//Set up a DateFormatter for input dates in "Internet" date format"
var inputFormatter = DateFormatter()
inputFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"
//Set up an output date formatter (in the local time zone)
var outputFormatter = DateFormatter()
outputFormatter.dateFormat = "MMM d yyyy, h:mm:ss a"
if let formattedtoDate = inputFormatter.date(from: str) {
let formattedtoString = outputFormatter.string(from: formattedtoDate)
print("'\(str)' = '\(formattedtoString)'")
} else {
print("Can't convert the string \(str) to a Date")
}
Edit:
(As Leo Dabus pointed out in a comment, you usually should not use a fixed dateFormat string in a DateFormatter that generates user-visible date strings. Better to use date and time styles and let the DateFormattter pick a specific format appropriate to the user's locale and language.)
This question already has answers here:
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 2 years ago.
I have strings like this 2020-09-21T21:13:55.636Z... I want to convert them into Dates and here's what I do;
func convertToDate(str: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter.date(from: str)!
}
The above code crashes on the unwrapping. I'm guessing it's because of the format... Any idea what I'm doing wrong?
For that date string you should use ISO8601DateFormatter, like this:
var str = "2020-09-21T21:13:55.636Z"
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
let date = formatter.date(from: str)
That will save you the trouble of trying to get a format string exactly right.
This question already has answers here:
Getting back a date from a string
(3 answers)
Closed 4 years ago.
I'm working on event application. I'm let the user create the event and save it in DB to remind him. now I'm facing an issue when the user put the date as string and from my code when I'm trying to convert it to date formate it gives me wrong date. for example if the user put in the text "01-03-2018 10:00" after convert it to date it returns to me "2017-12-24 10:00:00 +0000".
here is my code
let startDateFormatter = DateFormatter()
startDateFormatter.dateFormat = "dd-MM-YYYY HH:mm"
startDateFormatter.locale = Locale(identifier: "en_US_POSIX")
startDateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
let d = "01-03-2018 10:00”
let startDate = startDateFormatter.date(from: d)
print(startDate!)
so any idea please
Finally I found it. it regarding to date formatter, when I changed the date formatter form startDateFormatter.dateFormat = "dd-MM-YYYY HH:mm”
to
endDateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
endDateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
` worked with me. I shared the answer if anyone faced the same issue
This question already has answers here:
How to remove characters from a String in swift
(5 answers)
Closed 5 years ago.
How to filter English letters in String using Swift like this:
2017-08-23T13:00:00+08:00
Delete the 'T' in time string:
2017-08-23 13:00:00 +08:00
Try this code
func convertDateFormater(_ date: String) -> String
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy'T'hh:mm:ss z"
let date = dateFormatter.date(from: date)
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm:ss z"
return dateFormatter.string(from: date!)
}
This question already has answers here:
How can I convert string date to NSDate?
(18 answers)
Closed 6 years ago.
The timestamp from the php rest service is coming out in string format:
2016-06-08 09:03:59
How to convert it to NSDate in my iOS code. Swift answer preferred. Thanks!
Here is one way:
// Initialize Date string
var dateStr = "2016-06-08 09:03:59"
// Set date format
var dateFmt = NSDateFormatter()
dateFmt.timeZone = NSTimeZone.defaultTimeZone()
dateFmt.dateFormat = "yyyy-MM-dd HH:mm:ss"
// Get NSDate for the given string
var date = dateFmt.dateFromString(dateStr)
print(date!)
Date is now of type NSDate.