Date format gone wrong in swift - ios

I need to set a date in medium format like: Jul 17, 2016 4:30:00 PM,
but I get 07/17/2016 4:30:00 p.m.
Any help will be highly appreciated. Thanks in advance.
this is my code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
dateFormatter.timeStyle = NSDateFormatterStyle.MediumStyle
let mydate = dateFormatter.stringFromDate(NSDate())

For me, the code you are using does exactly what you need.
In case you need you can achieve this by using MMM dd, yyyy h:mm a as the format string for your formatter object.
Code follows:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy h:mm a"
let mydate = dateFormatter.stringFromDate(NSDate())
// "Jul 17, 2016 11:48 AM"

You just need to set your date formatter locale to "en_US_POSIX" and you should also set your formatter calendar to ISO8601 to get the result you were expecting:
Xcode 8 beta 2 • Swift 3
let dateFormatter = DateFormatter()
dateFormatter.calendar = Calendar(identifier: .ISO8601)
dateFormatter.locale = Locale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .medium
print(dateFormatter.string(from: Date())) // "Jul 17, 2016, 4:09:48 AM\n"
Xcode 7.3.1 • Swift 2.2.1
let dateFormatter = NSDateFormatter()
dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.dateStyle = .MediumStyle
dateFormatter.timeStyle = .MediumStyle
print(dateFormatter.stringFromDate(NSDate())) // "Jul 17, 2016, 4:12:49 AM\n"

Related

iOS 15.4 Date Format "Sep" now "Sept"

The app crashes once the user selects any date of September.
It only happens with iOS 15.4.
I have datePicker formatted date to return always in UTC format en_GB.
#objc func doneDateClicked() {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none //CHANGED 09.07.2020 date medium time none
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.timeZone = TimeZone(identifier: "GMT")
dateFormatter.locale = Locale(identifier: "en_GB")
dateFormatter.dateFormat = "dd MMM yyyy"
let timeFormatter = DateFormatter()
timeFormatter.dateStyle = .none
timeFormatter.timeStyle = .medium
timeFormatter.timeZone = TimeZone(secondsFromGMT: 0)
timeFormatter.timeZone = TimeZone(identifier: "GMT")
timeFormatter.locale = Locale(identifier: "en_150")
dateTextField.text = dateFormatter.string(from: (datePicker?.date )!)
self.view.endEditing(true)
}
Then I need to covert the selected date from string (dateTextField.text) to date
on iOS 15.4 the month September it shows "Sept".
code to convert from string to date
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_GB")
formatter.dateFormat = "dd MMM yyyy hh:mm:ss"
formatter.calendar = Calendar(identifier: .gregorian)
let datenN = formatter.date(from: "\(dateTextField.text ?? "") 00:00:00")
datenN crashing the app it returns nil
I tried to use code formatter.locale = Locale(identifier: "en_US_POSIX") but the app still crashing
Assuming this problem occurs just for September and on iOS 15.4, then
a quick work around, is to replace "Sept" with "Sep", using
dateTextField.text = dateFormatter.string(from: (datePicker?.date )!)
if dateTextField.text.contains("Sept") {
dateTextField.text = dateTextField.text.replacingOccurrences(of: "Sept", with: "Sep")
}

Swift convert datetime format to another format

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

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

NSDate to String with TimeZone Offset

I've the following code
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
dateFormatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
dateFormatter.stringFromDate(NSDate())
It produces date in the following format:
2015-08-06T15:44:49+0000
But I want it to be in the following format 2015-08-06T15:44:49+00:00
As you can see the offset needs to b +00:00 instead of +0000
You need to use "xxx" instead of "Z".
edit/update Swift 3 or later:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.calendar = Calendar(identifier: .gregorian)
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssxxx"
dateFormatter.string(from: Date()) // "2019-03-22T14:36:07+00:00"
If you need some reference you can use this:

Resources