How to convert string to date to string in Swift iOS? [duplicate] - ios

This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 7 years ago.
Am learning swift and am struck in converting the date String to NSDate to string. Am getting the date string in this format "Thu, 22 Oct 2015 07:45:17 +0000". I need to show the date in the MM-dd-yyyy format. I tried the following code but, it returns "null".
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
let dateObj = dateFormatter.dateFromString(dateString!)
print("Dateobj: \(dateObj)")
Can anyone please help where am going wrong? Looking forward the help. Thanks in advance.

First, you need to convert your string to NSDate with its format. Then, you change the dateFormatter to your simple format and convert it back to a String.
Swift 3
let dateString = "Thu, 22 Oct 2015 07:45:17 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, dd MMM yyyy hh:mm:ss +zzzz"
dateFormatter.locale = Locale.init(identifier: "en_GB")
let dateObj = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "MM-dd-yyyy"
print("Dateobj: \(dateFormatter.string(from: dateObj!))")
The printed result is: Dateobj: 10-22-2015

//String to Date Convert
var dateString = "2014-01-12"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let s = dateFormatter.dateFromString(dateString)
println(s)
//CONVERT FROM NSDate to String
let date = NSDate()
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
var dateString = dateFormatter.stringFromDate(date)
println(dateString)

Swift 2 and below
let date = NSDate()
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
var dateString = dateFormatter.stringFromDate(date)
println(dateString)
And in Swift 3 and higher this would now be written as:
let date = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
var dateString = dateFormatter.string(from: date)

See answer from Gary Makin. And you need change the format or data. Because the data that you have do not fit under the chosen format. For example this code works correct:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
let dateObj = dateFormatter.dateFromString("10 10 2001")
print("Dateobj: \(dateObj)")

Related

Date formatter not changing properly

The date I'm having is this..
2021-03-05T12:21:18
But I want it in dd-MM-yyyy format. This is what I did for that...
dateFormatter.dateFormat = "YYYY-MM-dd'T'HH:mm:ss"
let date2 = dateFormatter.date(from: self.Due_Date) //self.Due_Date is 2021-03-05T12:21:18
dateFormatter.dateFormat = "dd-MM-yyyy"
self.Due_Date = dateFormatter.string(from: date2!) //CRASH HERE
But I get date2 as nil. So the code crashes at self.Due_Date = dateFormatter.string(from: date2!)
What could be the reason for this...?
The “yyyy” represents the calendar year of the date while the “YYYY” represents the year of the week.
for detail information, please see this Difference between 'YYYY' and 'yyyy' in NSDateFormatter
let getInputString = "2021-03-05T12:21:18"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
if let getInputDate = dateFormatter.date(from:getInputString){
dateFormatter.dateFormat = "dd-MM-yyyy"
let getoutputString = dateFormatter.string(from: getInputDate)
}
Note : as per #LeoDabus guidelines please ensure the self.Due_Date is empty or not before start the conversion.

Convert string to particular date format getting nil in iOS

I am getting date in string format from server. So I am trying to formatting to particular format.
But it is getting nil.
"07September16 4:09 am", This I want to format like Sep 07 2016
I am doing it with following code
dateLabel.text = model().formatStringToDate(dateString: date)
func formatStringToDate(dateString: String) -> String {
let dateStr = dateString
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM d yyyy"
let formattedDate = dateFormatter.date(from: dateStr)
print("formattedDate \(formattedDate)")
//I have to return formatted string here
}
Any suggestions?
That should work;
// create `Date` from date string
let dateStr = "07September16 4:09 am"
let dateFormatterGet = DateFormatter()
dateFormatterGet.locale = Locale(identifier: "en_US_POSIX")
dateFormatterGet.dateFormat = "ddMMMMyy h:mm a"
let date = dateFormatterGet.date(from: dateStr)
// convert back Date to string as Sep 07 2016
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "MMM dd yyyy"
print(dateFormatterPrint.string(from: date!))
let dateString = "07September16 4:09 am"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "ddMMMyy h:mm a"
let dateObj = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "MMM dd yyyy"
print("Dateobj: \(dateFormatter.string(from: dateObj!))")
First you must get date from your string with current format like`
let dateStr = dateString
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "ddMMMMyy h:mm a"
let formattedDate = dateFormatter.date(from: dateStr)!
print("formattedDate \(formattedDate)")
After getting the date you can get date string in which format you want just change the format`
dateFormatter.dateFormat = "MMM dd YYYY"
let convertedDateString = dateFormatter.string(from: formattedDate)

Can't find the right date formatter (DateFormatter) in iphone

I am getting date string from server like that "2017-08-05T00:30:00.000+02:00". Below is my code for date formatter.
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"
dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
But it returns 2017-08-04 22:30:00 +0000. Which is wrong. Any suggestions?
In Swift 3
You can change your server time string to UTC time Date as:
let dateFormatter: DateFormatter = DateFormatter()
var serverDateString = "2017-08-05T00:30:00.000+02:00"
let index = serverDateString.index(serverDateString.startIndex, offsetBy: 19)
serverDateString = serverDateString.substring(to: index)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone.init(identifier: "UTC")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Your Code for parsing date is correct: 2017-08-05T00:30:00.000+02:00 and 2017-08-04 22:30:00 +0000 represent the same time, just in different time zones. Your only problem is that Date doesn't actually store the time zone
yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ
This is correct date formate for your input string. Here is the Apple Document for more description.
Here is my code:
var strInputDateString: String = "2017-08-05T00:30:00.000+02:00"
var dateFormat = DateFormatter()
dateFormat.dateFormat = "yyyy-MM-dd'T'hh:mm:ss.SSSZZZZZZ"
//Set new dateFormate
var date1: Date? = dateFormat.date(from: strInputDateString)
dateFormat.dateFormat = "dd-MM-YYYY hh:mm:ss"
// Your Desire
var strOutputDateString: String = dateFormat.string(from: date1!)
print("\(strInputDateString)")
print("\(strOutputDateString)")
output:
Main input String:2017-08-05T00:30:00.000+02:00
Converted String: 05-08-2017 04:00:00
For now, I tried to convert your date format with my custom. It's give me the perfect output.
Actually, your code is good.
I copied your data and ran it in playground.
What kind of format you want it be?
Try this
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
dateFormatter.locale = Foundation.Locale(identifier: "en_US_POSIX")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
Hope this helps
The step you are missing is to again use your DateFormatter to format the date into a string. When you use print on the date, it will always show GMT. For illustration purposes, I've set the TimeZone to Berlin to match the original offset of +2:00.
let dateFormatter: DateFormatter = DateFormatter()
let serverDateString = "2017-08-05T00:30:00.000+02:00"
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZZ"
// Hard set to Berlin
dateFormatter.timeZone = TimeZone(identifier: "Europe/Berlin")
let currentDate: Date = dateFormatter.date(from: serverDateString)!
// Your missing step
dateFormatter.string(from: currentDate)
Remember that DateFormatters are about:
Parsing from a String - df.date(from: String)
Formatting from a Date - df.string(from: Date)

Format Date from String - swift3

HI I am trying to formate date from string (08-05-1988) I want to convert to date as given below, output also given below,
let dateString = "08-05-1988"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateFromString = dateFormatter.date(from: dateString)
let stringValue = String(describing: dateFromString)
out put : Optional(1988-05-07 18:30:00 +0000)
Then, I want to format the out put date from (1988-05-07 18:30:00 +0000) to other format as given below. But, when format the string to Date is nil
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-mm-dd hh:mm:ss Z"
let date = dateFormatter.date(from: dateString)
let formatter = DateFormatter()
formatter.dateFormat = "dd-MMM-yyyy"
let formatedDate:String = formatter.string(from: date!)
Expected Output : 08-May-1988
The output Optional(1988-05-07 18:30:00 +0000) is the result of the description of a Date object. It's not related to any given string format.
To convert 08-05-1988 to 08-May-1988 you can simply use
let dateString = "08-05-1988"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
let dateFromString = dateFormatter.date(from: dateString)
dateFormatter.dateFormat = "dd-MMM-yyyy"
let formatedDate = dateFormatter.string(from: dateFromString!)
let date = Date()
let format = DateFormatter()
format.dateFormat = "dd-MM-yyyy"
let formatDate = format.string(from: date)
You're calling dateFormatter.date on dateString instead of dateFromString. Since "08-05-1988" doesn't match the specified format of "yyyy-mm-dd hh:mm:ss Z", dateFormatter.date returns nil.

Use Date Formatter iOS10 Swift3

How to use Date Formatter in ios 10 swift 3?
i
You'll have to first convert the string to a date and then to a string in your required format. You can use DateFormatter to do this. Check the below code
let date = "07-11-2016 12:00:00"
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let convertedDate = formatter.date(from: date)
formatter.dateFormat = "d MMMM yyyy h a"
let dateString = formatter.string(from: convertedDate!)
print(dateString) // Output will be 7 November 2016 12 PM
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let date = dateFormatter.date(from: "07-11-2016 12:00:00")
dateFormatter.dateFormat = "dd MMMM yyyy HH a"
let dateString = dateFormatter.string(from: date)
dateString will be you desired output format of you date.
or you can simply create an extension of Date or string that return you the formatted date.

Resources