Swift convert datetime format to another format - ios

My scenario, I am trying to converting Oct 31, 2019 5:20 PM (MMM dd, yyyy) to 2019-10-31 05:20:00 (yyyy-MM-dd h:mm:ss)
String DateTime: Oct 29, 2019 11:00 PM
Need to convert: "yyyy-MM-dd h:mm:ss"
let formatter = DateFormatter() // Converting 2019-10-29 to Oct 29, 2019
formatter.dateFormat = "yyyy-MM-dd"
let yourDate = formatter.date(from: fullDate)
formatter.dateFormat = "yyyy-MM-dd h:mm:ss"
let myStringafd = formatter.string(from: yourDate!)
print(myStringafd)

It crashes because your datestring format and the format in which you want to convert is different. Change the dateFormat to dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
Here is the code:-
let dateString = "2020-05-01 11:00 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.locale = Locale.init(identifier: "en_GB")
let dateres = dateFormatter.date(from: dateString)
print(dateres)

You have to first create date from the available date string. And then convert it back to string with the required dateFormat as below,
let dateString = "Oct 29, 2019 11:00 PM" // Or "Oct 31, 2019 5:20 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy hh:mm a"
dateFormatter.locale = Locale(identifier: "en_GB")
let stringTodate = dateFormatter.date(from: dateString)
print(stringTodate!.description(with: dateFormatter.locale)) //Tuesday, 29 October 2019 at 23:00:00
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss"
let finalDate = dateFormatter.string(from: stringTodate!)
print(finalDate) // 2019-10-29 11:00:00
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
let finalDate1 = dateFormatter.string(from: stringTodate!)
print(finalDate1) // 2019-10-29 11:00 pm

Related

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)

iOS Date timezone conversion not working

In my playground, I'm testing conversion of date time but it doesn't seem to be converting:
import UIKit
let dateString = "2017-12-01 10:00:00 +0000"
print("original date = \(dateString)")
var formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.timeZone = TimeZone(identifier: "Asia/Shanghai")
if let date = formatter.date(from: dateString) {
print("converted date = \(date)")
}
print("test")
The output is:
original date = 2017-12-01 10:00:00 +0000
converted date = 2017-12-01 10:00:00 +0000
test
I'm expecting the converted date to say something like 2017-12-01 18:00:00 +0800 (since Shanghai is 8 hours ahead of UTC 00:00:00)
Please try this:
let dateString = "2017-12-01 10:00:00 +0000"
print("original date = \(dateString)")
var formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
formatter.timeZone = TimeZone.init(abbreviation: "UTC")
if let date = formatter.date(from: dateString) {
formatter.timeZone = TimeZone(identifier: "Asia/Shanghai")
let localTime = formatter.string(from: date)
print(localTime)
}
print("test")
Output:
original date = 2017-12-01 10:00:00 +0000
2017-12-01 18:00:00 +0800
test
GMT:
formatter.timeZone = TimeZone(abbreviation: NSTimeZone.local.abbreviation()!)
UTC:
formatter.timeZone = TimeZone(abbreviation: NSTimeZone.default.abbreviation()!)
To display local datetime, i use the GMTTimeZone.

Sometimes NSTimeZone doesn't return proper UTC value?

I have encountered with below lines of code sometimes doesn't return proper UTC value:
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
formatter.timeZone = NSTimeZone(abbreviation: "UTC")
Returns:
"2016-12-11 2:07:35 am +0000"
Instead of:
"2016-12-11 02:07:35 +0000"
What's wrong with that?
please use these code
let date = NSDate()
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
formatter.timeZone = NSTimeZone.localTimeZone()
formatter.locale = NSLocale.currentLocale()
print(formatter.stringFromDate(date))

converting String to Date swift3 ios10

why does this return a nil? detailDate is in this format:
"Wednesday, November 23, 2016 at 2:01 PM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy' at 'h:mm a"
dateFormatter.dateStyle = .long
let dateForCalendar = dateFormatter.date(from: detailDate!)
I believe it returns a nil because my dateFormat is not the same as what my detailDate format is, but I don't understand why it's different.
After assigning a DateFormat why are you changing the date style ?
remove,
dateFormatter.dateStyle = .long
and it will work,
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy' at 'h:mm a"
let dateForCalendar = dateFormatter.date(from: detailDate!)
let detailDate = "Wednesday, November 23, 2016 at 2:01 PM"
let dateformatter = DateFormatter()
dateformatter.dateFormat = "EEEE, MMMM dd, yyyy' at 'h:mm a"
let strDate = dateformatter.date(from: detailDate)
print(strDate!)
try this , it's work proper

How to change string to date without converting to any timezone in Swift?

I know there are lot of questions passing around over this simple issue, but still I couldn't get a clear idea.
Here is what I want:
SelectedDateString = "19-08-2015 09:00 AM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm a"
dateFormatter.timeZone = NSTimeZone();
let SelectedUTCDate = dateFormatter.dateFromString(SelectedDateString)!
println("SelectedLocalDate = \(SelectedLocalDate)")
// OUTPUT: SelectedLocalDate = 2015-08-18 18:30:00 +0000
If I dont use TimeZone:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm a"
// dateFormatter.timeZone = NSTimeZone();
let SelectedUTCDate = dateFormatter.dateFromString(SelectedDateString)!
println("SelectedLocalDate = \(SelectedLocalDate)")
//OUTPUT: SelectedLocalDate = 2015-08-18 19:26:00 +0000
Why is there change in the time and Date? What I want is:
//OUTPUT: SelectedLocalDate = 2015-08-19 09:00:00 +0000
Also I want to convert the Local Date to Exact UTC Date
//Like this: SelectedUTCDate = 2015-08-19 03:30:00
inputString = "19-08-2015 10:45 am" // My localtime is 10:35 AM, so I set 10 mins from now
var currentUTCTime = NSDate() // currentUTCTime is 05: 15 AM.
I want to convert the inputString to its respective UTC Time and find the difference between the two times in both date and string.
//Like this Date: diffInDate: 00-00-0000 00:10 and
// Like this String: diffInString: 10 mins
How can I get both of these?
let dateString = "19-08-2015 09:00 AM"
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm a"
if let dateFromString = dateFormatter.date(from: dateString) {
print(dateFromString) // "2015-08-19 09:00:00 +0000"
dateFormatter.timeZone = .current
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm a Z"
dateFormatter.string(from: dateFromString) // 19-08-2015 06:00 AM -0300"
}
extension Date {
func originToString(dateFormat: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = dateFormat
dateFormatter.calendar = Calendar(identifier: .iso8601)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
return dateFormatter.string(from: self)
}
}

Resources