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.
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:
Converting a String to DateTime
(17 answers)
Closed 2 years ago.
I want to convert different date format to current date format
Example :
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("tr-TR");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("tr-TR");
string dateString = "16.03.2020 10:50:00"; //(I record the value) (now format dd.MM.yyyy HH:mm:ss)
//Change Culture (en-US)
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Datetime convertedDate = Convert.ToDatetime(dateString); //(now format dd/MM/yyyy HH:mm:ss)
error = String was not recognized as a valid DateTime
how can i solve ?
I solved it this way
string dateString = "16.03.2020 10:55:10"; //Çağdaş SANCARBARLAZ
DateTime lastCheckDate = Convert.ToDateTime(dateString,
System.Globalization.CultureInfo.GetCultureInfo("tr-TR").DateTimeFormat);
Result :
Thank you everyone
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:
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))
}