Converting string to date returns nil yyyMMdd - swift

I am trying to convert my string to a date using a date formatter.
let timeSTR = "19740707"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyyMMdd"
let date = dateFormatter.date(from:timeSTR)
i am getting nil in the date object. any other timeSTR for example "19740706" or "19740705" or any other option will work ...
we found that if we will add the following command
dateFormatter.timezone = Timezone(identifier:"UTC")
it's fix the problem ...
more similar date that will return nil are: 19560603 , 19440401, 19400601
any one have an idea what wrong with those values???
Adding Print screen from my Playground with value 19740707:
Adding Print screen from my Playground with value: "19740706"

Related

How to format stringDateJson to label? [duplicate]

This question already has answers here:
Convert string to date in Swift
(18 answers)
Closed 1 year ago.
var str = "2021-05-23T06:35:47.409Z"
var formatter = DateFormatter()
formatter.dateFormat = "MMM d yyyy, h:mm:ss a"
let formattedtoDate = formatter.date(from: str)
let formattedtoString = formatter.string(from: formattedtoDate) //Error Cannot force unwrap value of non-optional type 'String'
cell.date_announce.text = formattedtoString
I'm trying to format sting to Date() and format Date to String in order to set value to date_announce label. Can anyone help me please?
If you break down what you are trying to do, there are actually 2 steps that require two different date formatters.
Convert an input date string (e.g. "2021-05-23T06:35:47.409Z") to a Date
Convert the Date to an output string in a different format.
Try this code instead:
var str = "2021-05-23T06:35:47.409Z"
//Set up a DateFormatter for input dates in "Internet" date format"
var inputFormatter = DateFormatter()
inputFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ"
//Set up an output date formatter (in the local time zone)
var outputFormatter = DateFormatter()
outputFormatter.dateFormat = "MMM d yyyy, h:mm:ss a"
if let formattedtoDate = inputFormatter.date(from: str) {
let formattedtoString = outputFormatter.string(from: formattedtoDate)
print("'\(str)' = '\(formattedtoString)'")
} else {
print("Can't convert the string \(str) to a Date")
}
Edit:
(As Leo Dabus pointed out in a comment, you usually should not use a fixed dateFormat string in a DateFormatter that generates user-visible date strings. Better to use date and time styles and let the DateFormattter pick a specific format appropriate to the user's locale and language.)

Swift DateTime Formatter returning nil with matching format

When parsing an input datetime string with the matching format, I am still getting nil back from DateFormatter in swift.
Input string (copied at runtime from variable) and confirmed that is the string running the call with Postman.
"2020-06-30T14:41:33.9"
Parsing method
static func FormatDateTimeString(dateTimeString: String) -> String {
let inputDateFormatter = DateFormatter()
inputDateFormatter.dateFormat = "YYYY-MM-DDThh:mm:ss.s"
let date = inputDateFormatter.date(from:dateTimeString) //date is always nil
let outputDateFormatter = DateFormatter()
outputDateFormatter.dateFormat = "MMM d, yyyy HH:mm"
return outputDateFormatter.string(from: date!)
}
I have also tried "YYYY-MM-DD'T'hh:mm:ss.s" and "YYYY-MM-DDThh:mm:ss" but none seem to work.
EDIT: Trying the format suggested below I am still getting nil.
EDIT: As Suggested from the comments I am still getting nil. Format below is: inputDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"
EDIT: Still getting null using the following config
Use this format:
"yyyy-MM-dd'T'HH:mm:ss.S"
Demo
let dateTimeString = "2020-06-30T14:41:33.9"
let inputDateFormatter = DateFormatter()
inputDateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.S"
let date = inputDateFormatter.date(from: dateTimeString) // "Jun 30, 2020 at 2:41 PM"

Using DateFormatter getting EXC_BAD_ACCESS code=2, works fine in playground Swift

I'm trying to convert a string that contains a time and date stamp in the format like this "02/03/19 11:00:00", I'm able to use DateFormatter to convert it to a Date format in playground fine, but as soon as I try to use the code in my project it throws a EXC_BAD_ACCESS. The function that I'm using is as follows
func convertStringToDate(dateString : String) -> Date {
var theDate : Date = Date.init()
print(dateString)
let theDateFormat = DateFormatter()
theDateFormat.locale = Locale(identifier: "en_AU")
theDateFormat.dateFormat = "dd-MM-yy' 'HH:mm:ss"
theDateFormat.timeZone = TimeZone(secondsFromGMT: 0)
theDate = theDateFormat.date(from: dateString) ?? Date.init()
return theDate
}
I'm just hoping that someone may have a couple of ideas that I can try to get past this roadblock.

Parse yyyy:MM:dd:hh:mm:ss using DateFormatter in Swift

I would like to parse Year:Month:Day:Hour:Minute:Second (e.g. 2017:01:01:23:59:59) in Swift. I am using the following dateFormat: yyyy:MM:dd:hh:mm:ss though the date formatter returns nil when given a string:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy:MM:dd:hh:mm:ss"
dateFormatter.date(from: "2017:01:01:23:59:59") // nil
NSDateFormatter.com says there is something invalid in either my date or format. What am I doing wrong?
I believe your format is incorrect. It should be yyyy:MM:dd:HH:mm:ss instead. HH for hours.

dateFromString returns nil every time format possibly wrong?

I am trying to use date from string method like below:
var dateNow = self.tags![indexPath.row].date!
let timeZone = NSTimeZone(name: "UTC")
let df = NSDateFormatter()
df.locale = NSLocale.currentLocale()
df.timeZone = timeZone
df.dateFormat = "yyyy-MM-ddThh:mm"
let date: NSDate = df.dateFromString(dateNow)! //this is nil and throws an unwrapping error
The date is formatted like this
dateNow String "2015-04-19T17:00:42.205Z"
Im guessing maybe that is formatted wrong but I have no idea how to write the df.dateFormat
you just have to format it as follow:
df.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Yet another way to get nil is if you use hh and your hours are on the 24 hr clock and > 12, in that case, you need HH (or H, for zero-padded).
That is:
Format: yyyy-MM-DD hh:mm:ss, string: "2016-03-01 13:42:17" will return nil .
Format: yyyy-MM-DD HH:mm:ss, string: "2016-03-01 13:42:17" will return the date you expect.