Wrong behavior with pt-BR DateFormatter Swift [duplicate] - ios

This question already has answers here:
1st april dates of 80s failed to parse in iOS 10.0
(1 answer)
Why NSDateFormatter is returning null for a 19/10/2014 in a Brazilian time zone?
(3 answers)
DateFormatter's returns nil for specific date strings without time in Swift
(1 answer)
Closed 3 years ago.
I have this method that parse a string Date:
let dateFormatterGet = DateFormatter()
let stringDate = "31/01/1965"
dateFormatterGet.dateFormat = "dd-MM-yyyy"
dateFormatterGet.locale = Locale(identifier: "pt_BR")
let birthDate = dateFormatterGet.date(from: stringDate)
print(birthDate)
If Date is 31/01/1965 (January 31st), the birthDate is returning nil, but if i change the day for 30, all works fine. What is wrong here?

Related

How to convert a time string to 12 hour time in swift? [duplicate]

This question already has answers here:
Xcode Swift am/pm time to 24 hour format
(12 answers)
Closed 1 year ago.
import Foundation
let dateFormatter = DateFormatter()
var timy = "13:25:00"
dateFormatter.dateFormat = "HH:mm:ss "
print(dateFormatter.date(from: timy))
// this gives me output of differnet time i.e Optional(2000-01-01 07:55:00 +0000)
This is the correct format to account for AM/PM:
dateFormatter.dateFormat = "h:mm:ss a"

What is the correct date format for this date-as-a-string? [duplicate]

This question already has answers here:
How to convert "2017-07-11T06:52:15.948Z" in to like "JUL, 7 2017" in swift
(1 answer)
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 1 year ago.
The back end is providing me with this date-as-a-string: 2021-09-10T12:57:01.671Z
I need to convert that string to a Date using the iOS DateFormatter; and to do so I need to set the formatter's dateFormat property.
I have tried all sorts of combinations, with no luck. Specifically I am struggling with the .671Z part.
What is the correct date format to use?
You need "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" as date format
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
guard let date = dateFormatter.date(from: self) else {
// fallback if date is not in correct format
return nil
}

DataFormatter.date(from string: String) -> Date? returns nil for some dates [duplicate]

This question already has answers here:
1st april dates of 80s failed to parse in iOS 10.0
(1 answer)
DateFormatter date from string returns nil
(1 answer)
DateFormatter's returns nil for specific date strings without time in Swift
(1 answer)
Closed 4 years ago.
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .short
dateFormatter.timeStyle = .none
let date = dateFormatter.date(from: "Sep 1, 1906")
Sep 1, 1906 always return nil in America/Edmonton timezone and Apr 4, 1986 for Jordan/Amman.
Does anyone know why? what am I doing wrong?

Date From Timestamp Becomes Wrong [duplicate]

This question already has answers here:
NSDate timeIntervalSince1970 not working in Swift? [duplicate]
(3 answers)
Closed 4 years ago.
I am trying to convert timestamp to Date. But it returns wrong date.
I have a time stamp
1524637838000.0
Which returns 25-04-2018 12:00 as per this online converter
But I get wrong date when convert using my code
let date = Date(timeIntervalSince1970:1524637838000.0)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm"
let myDate = dateFormatter.string(from: date)
print("Date is = ",myDate)
I get
07-11-50283 12:03
as result. Is there anything wrong with my code?
your timestamp 1524637838000.0 in milliseconds, it should be in seconds, so your date initialization should be:
let date = Date(timeIntervalSince1970:(1524637838000.0/1000))

Swift DateFormatter could not produce date from string on iOS 9 [duplicate]

This question already has answers here:
DateFormatter doesn't return date for "HH:mm:ss"
(1 answer)
Using dateFormatter in another language [duplicate]
(1 answer)
NSDateFormatter returns nil in swift and iOS SDK 8.0
(2 answers)
Closed 5 years ago.
I'm trying to format a string(ex. Tue, 16 Jan 2018 | 05:34 PM) to date and then convert it back to string(ex. 2018-01-16 17:34:00) with different format by using following code.
let dateFormatterOne = DateFormatter()
dateFormatterOne.dateFormat = "E, d MMM yyy | hh:mm a"
dateFormatterOne.locale = Locale(identifier: "en_US_POSIX")
let dateFromString = dateFormatterOne.date(from: string)
let dateFormatterTwo = DateFormatter()
dateFormatterTwo.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = dateFormatterTwo.string(from: dateFromString!)
This seems to be working well in simulator but when I tested it on an iPod Touch running iOS 9 it doesn't give date in the first conversion so it becomes nil and crashes.

Resources