NSDateFormatter doesn't work on actual equipment [duplicate] - swift

This question already has answers here:
What is the best way to deal with the NSDateFormatter locale "feature"?
(4 answers)
Closed 7 years ago.
I tried to use NSDateFormatter like following.
On simulator, it works fine. -> 2015-06-01
But, on actual equipment, it does't work. it'll be nil.
let d = "Wed Jul 01 04:48:51 +0000 2015"
let formatter = NSDateFormatter()
formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
if let date = formatter.dateFromString(d) {
formatter.dateFormat = "yyyy-MM-dd"
self.dateLabel?.text = formatter.stringFromDate(date)
}
Is this iPhone's problem?

Could be an issue with locale and 12/24-hour time handling.
Try forcing formatter's locale to be consistent for the date you are receiving.
let d = "Wed Jul 01 04:48:51 +0000 2015"
let formatter = NSDateFormatter()
formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
if let date = formatter.dateFromString(d) {
formatter.locale = NSLocale.currentLocale()
formatter.dateFormat = "yyyy-MM-dd"
self.dateLabel?.text = formatter.stringFromDate(date)
}
Check these question/answers for more info.

Related

Date Formatter unexpected output

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "MMMM dd, yyyy 'at' HH:mm:ss aa z"
dateFormatter.amSymbol = "AM"
let dateString = "February 02, 2022 at 10:49:06 AM GMT+3"
print(dateFormatter.date(from: dateString)!)
Prints -> 2022-02-01 21:49:06 +0000
why this prints 21:49 instead of 10:49?
So, yes, we're both missing something. The time format should be use hh not HH (HH is for 24 hour time and some weird conversation was going in there)
So, I tested in a Playground with...
let threeHoursFromGMT = Measurement(value: 3, unit: UnitDuration.hours).converted(to: UnitDuration.seconds).value
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "MMMM dd, yyyy 'at' hh:mm:ss aa z"
dateFormatter.amSymbol = "AM"
dateFormatter.timeZone = TimeZone.init(secondsFromGMT: Int(threeHoursFromGMT))
let dateString = "February 02, 2022 at 10:49:06 AM GMT+3"
let date = dateFormatter.date(from: dateString)!
dateFormatter.string(from: dateFormatter.date(from: dateString)!)
And it generated
"Feb 2, 2022 at 6:49 PM" // let date = dateFormatter.date(from: dateString)!
"February 02, 2022 at 10:49:06 AM GMT+3" // dateFormatter.string(from: dateFormatter.date(from: dateString)!)
The first output is based on my timezone (+11) and the second is based on the formatter requirements (using +3)

Date conversion from string not happening [duplicate]

This question already has answers here:
Converting NSString to NSDate (and back again)
(17 answers)
Closed 4 years ago.
I have a date within order_date in this format May 31, 2018 at 3:35 PM
I’m converting it to Date like so…
if let order_date = value["order_date"] as? String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let date = dateFormatter.date(from: order_date)
}
But I'm getting date as nil. What could be the issue..?
your dateformat is wrong change your format to
dateFormatter.dateFormat = "MMMM dd, yyyy 'at' hh:mm a"
for e.g if you get the month as Mar then use - MMM, if you get March then use - MMMM
you can check the dateformat's in here
for full answer
if let order_date = value["order_date"] as? String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd, yyyy 'at' hh:mm a"
var date = dateFormatter.date(from: order_date)
if date == nil{
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
date = dateFormatter.date(from: order_date)
}
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let myStringafd = dateFormatter.string(from: date!)
print(myStringafd)
}
for detail dateforamt conversion you get here
Replace your code with below :
if let order_date = value["order_date"] as? String
{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, yyyy 'at' hh:mm a"
let date = dateFormatter.date(from: order_date)
}

Remove Optional string for date in UIDatePicker

Happy New Year, all!
How do I get a datePicker text to show like this Jan 1, 2017 instead of 2017/1/1? In my PlayGround, it looks as expected but in my simulator or console, it looks not expected.
// createDatePicker():
datePicker.datePickerMode = .date
// donePressed():
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .none
dateFormatter.dateFormat = "mm/dd/yyyy"
let fromString = dateFormatter.string(from: datePicker.date)
let date = dateFormatter.date(from: fromString) // I dont need time but got Jan 1, 2017, 12:00 AM in playground
datePickerText.text = "\(date)"
print("\(date)") // in console: Optional(2017-1-1 00:00:00 +0000)
I need to get rid of the Optional() but not sure how to convert Date! to String!. How to achieve getting the text to be Jan 1, 2017?
In your code you converting Date to date String: -
let fromString = dateFormatter.string(from: datePicker.date)
let date = dateFormatter.date(from: fromString) //Avoid this line in your code
then no need to change again date string into date try below code: -
let formatter = DateFormatter()
formatter.dateFormat = "MMM d, yyyy" //Date Formatting
let date = formatter.string(from: datePicker.date)
datePickerText.text = date
print(date)
Output:-
Jan 1, 2017
Just format your date how you want it to be, like this:
dateFormatter.dateFormat = "MMM d, yyyy"
let fromString = dateFormatter.string(from: datePicker.date)
print(fromString) // Jan 1, 2017
You can try this!
let formatter = NSDateFormatter()
formatter.dateformat = "MMMM dd,YYYY"
if there is any label to which your are trying to show the date format then,
label.text = formatter.stringFromDate(datepicker.date)

Using dateFormatter's on swift

var dateString = "Sat, 18 Jun 2016 11:00:00 +0900"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
var dateFromString = dateFormatter.dateFromString(dateString)
dateFormatter.locale = NSLocale(localeIdentifier: "ko_KR")
dateFormatter.dateStyle = .FullStyle
var dateFromStringToKorean = dateFormatter.stringFromDate(dateFromString!)
This code working in Playground.
but When I build this code in my iPhone it doesn't work.
Could you tell me what's the problem of this code.
Set locale property of dateFormatter before using it, as below:
var dateString = "Sat, 18 Jun 2016 11:00:00 +0900"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
dateFormatter.locale = NSLocale(localeIdentifier: "ko_KR")
dateFormatter.dateStyle = .FullStyle
var dateFromString = dateFormatter.dateFromString(dateString)
var dateFromStringToKorean = dateFormatter.stringFromDate(dateFromString!)

Get NSDate and convert it from the 24 hour format to 12 hour format in swift

I've seen this post for other languages but not for swift. I have a date saved in the format of 2015-08-31 21:36:00 +0000 and I'm able to extract the day, month, year and weekday with the code below to produce Monday, August 31, 2015. When I try to use:
let hourInt = components.hour
var hourString = String(hourInt)
It prints a four hour difference. In this case "17" for the "21". How do I
display it as 9:36 P.M.?
let flags: NSCalendarUnit = NSCalendarUnit.CalendarUnitMinute | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitWeekday | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear
let date = NSDate()
let dateFormatter: NSDateFormatter = NSDateFormatter()
let components = NSCalendar.currentCalendar().components(flags, fromDate: array.date)
let weekday = components.weekday
let weekdays = dateFormatter.weekdaySymbols
let weekdayString = weekdays[weekday-1] as! String
let month = components.month
let months = dateFormatter.monthSymbols
let monthString = months[month-1] as! String
let dayInt = components.day
var dayString = String(dayInt)
let year = components.year
let yearString = String(year)
println(weekdayString + ", " + monthString + " " + dayString + ", " + yearString)
Your date string suffix +0000 means it is UTC time if you want to display time at UTC you need to specify it when setting your date formatter.
let dateString = "2015-08-31 21:36:00 +0000"
let df = NSDateFormatter()
df.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
if let date = df.dateFromString(dateString) {
// now you have your date object
// to display UTC time you have to specify timeZOne UTC
df.timeZone = NSTimeZone(forSecondsFromGMT: 0)
df.dateFormat = "EEEE, MMMM dd, yyyy h:mm:ss a"
let stringFromDate = df.stringFromDate(date)
println(stringFromDate) // "Monday, August 31, 2015 9:36:00 PM"
}
If you want to strip the time zone information, pass the GMT time zone.
This code does quite the same as yours
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy"
dateFormatter.timeZone = NSTimeZone(abbreviation: "GMT")
println(dateFormatter.stringFromDate(array.date))
Setting locale of NSDateFormatter to en_US_POSIX fixed issue for me.
let sharedFormatter:NSDateFormatter = {
let formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier:"en_US_POSIX")
return formatter
}()
sharedFormatter.dateFormat = "MMM d YYYY, h:mm a"
let dateString = sharedFormatter.stringFromDate(NSDate())
// Aug 2 2016, 5:45 PM
Swift 5.0
let dateString = "2015-08-31 21:36:00 +0000"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
if let date = dateFormatter.date(from: dateString) {
// now you have your date object
// to display UTC time you have to specify timeZOne UTC
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "EEEE, MMMM dd, yyyy h:mm:ss a"
let stringFromDate = dateFormatter.string(from: date)
print(stringFromDate) // "Monday, August 31, 2015 9:36:00 PM"
}