getting nil date from string using formatter [duplicate] - ios

This question already has an answer here:
1st april dates of 80s failed to parse in iOS 10.0
(1 answer)
Closed 5 years ago.
I'm using this code for converting array of String date into Date
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
for i in 0 ..< data.count {
let time = data[i]
let ft_date = time["ft_date"] as! String
print(ft_date)
let dateF = formatter.date(from: ft_date)
print(dateF)
}
and it's the output of console
2017-04-09T00:00:00
Optional(2017-04-08 19:30:00 +0000)
2017-04-08T00:00:00
Optional(2017-04-07 19:30:00 +0000)
2017-04-05T00:00:00
Optional(2017-04-04 19:30:00 +0000)
2017-04-01T00:00:00
Optional(2017-03-31 19:30:00 +0000)
2017-04-01T00:00:00
Optional(2017-03-31 19:30:00 +0000)
2017-04-01T00:00:00
Optional(2017-03-31 19:30:00 +0000)
2017-03-22T00:00:00
nil
why this is happening? it's the same format but I'm getting nil

Are you living in a country where daylight saving time changes on 2017-03-22 at midnight?
If yes this could be the reason because the date does not exist.

You need to change time Format ;
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"

Related

date format error in swift with month & year [duplicate]

This question already has answers here:
Difference between 'YYYY' and 'yyyy' in NSDateFormatter
(4 answers)
Closed 1 year ago.
Wrong value recived when decoding date in swift
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "LLL YYYY"
dateFormatterGet.timeZone = TimeZone(abbreviation: "UTC")
dateFormatterGet.locale = Locale(identifier: "en_US_POSIX")
if let date = dateFormatterGet.date(from: "Mar 2020") {
print(dateFormatterGet.string(from: date))// Prints Dec 2019
print(date)//2019-12-22 00:00:00 +0000
} else {
print("There was an error decoding the string")
}
it prints 2019-12-22 00:00:00 +0000
Should the dateFormat be dateFormatterGet.dateFormat = "LLL yyyy"?
This prints
Mar 2020
2020-03-01 00:00:00 +0000
in my console.

How to convert string to date correctly? [duplicate]

This question already has answers here:
NSDateFormatter still parsing instead having incorrect format
(3 answers)
Closed 3 years ago.
Why does dateFormatter return the correct date from an invalid format string?
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2020/////07////////10") //"Jul 10, 2020 at 12:00 AM"
I will say more - it also works unexpectedly
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let dotDate = dateFormatter.date(from: "2020....07...10") // Optional(2020-07-09 21:00:00 +0000)
let commaDate = dateFormatter.date(from: "2020,,,,07,,,,10") // Optional(2020-07-09 21:00:00 +0000)
My version is probably the issue in internal implementation on Apple side and comparison with the ASCII code table, where the codes of these characters (,,-,.,/) are in order (from 44 to 47)
This is probably a part of their algorithm.
If you want the formatter to return nil in this case you can change the dateFormat to:
dateFormatter.dateFormat = "yyyy/MM/dd"
Results
let date = dateFormatter.date(from: "2020/////07////////10") // nil
let date2 = dateFormatter.date(from: "2020/07/10") // Jul 10, 2020 at 12:00 AM"
let date3 = dateFormatter.date(from: "2020.07.10") // Jul 10, 2020 at 12:00 AM"
I did run your code and it gives nil
Whereas if I enter correct date format then I receive the output
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: "2020-7-10")
print(date) // Optional(2020-07-10 00:00:00 +0000)

Issue in formatting date string Swift 3 [duplicate]

This question already has answers here:
NSDateFormatter doesn't show time zone abbreviation for "Asia/Kolkata" for the "z" or "zzz" specifier, just the GMT offset
(1 answer)
What is the best way to deal with the NSDateFormatter locale "feature"?
(4 answers)
Closed 5 years ago.
I need to convert the following date string in to a Date in Swift 3.
Fri Dec 09 16:18:43 AMST 2016
Here is the code that i have been using, but it's getting cash on this particular date string conversion. (This date was logged on Android using new Date().toString() method.)
static func formatDate(date: String) -> String {
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
//Works for "Fri Sep 16 10:55:48 GMT+05:30 2016"
var myDate = dateFormatter.date(from: date)
// My date returns nil on "Fri Dec 09 16:18:43 AMST 2016"
dateFormatter.dateFormat = "dd/MM/yyyy"
return "\(dateFormatter.string(from: myDate!))"
}
There are both type of strings in the database. I tried with various types of Timezone formats (z/zz/zzz/zzzz/zzzzz) but always myDate returns nil.
Any help would be appreciated.
Thanks In Advance.
Apple doc for TimeZone(abbreviation:):
In general, you are discouraged from using abbreviations except for unique instances such as “GMT”. Time Zone abbreviations are not standardized and so a given abbreviation may have multiple meanings.
Does AMST represents "Amazon Summer Time" (UTC-3) or "Armenia Summer Time" (UTC+5)? See: https://www.timeanddate.com/time/zones
That's probably why it can't detect the proper timezone to use.
Solutions I can propose:
If you know which timezone AMST is:
replace AMST by UTC-3 or UTC+5 in the date string
remove AMST from the date string and use dateFormatter.timeZone = TimeZone(secondsFromGMT: -3 or 5 * 3600)
Have your source output a more precise timezone.
Note the following code, where AMST is understood correctly:
let df = DateFormatter()
df.locale = Locale.init(identifier: "pt_BR") // assuming AMST is Amazon Summer Time (UTC -3)
df.dateFormat = "HH:mm:ss z"
print(df.date(from: "16:18:43 AMST")) // Optional(2000-01-01 19:18:43 +0000)
But as soon as you include English day or month names (e.g. Fri or Dec) it will produce nil (because they aren't in Portuguese).

How to convert milliseconds to date string in swift 3 [duplicate]

This question already has answers here:
NSDate timeIntervalSince1970 not working in Swift? [duplicate]
(3 answers)
Closed 6 years ago.
I am trying to convert milliseconds to date string in swift 3,i tried by setting date fomatter but i am not getting current date string.
var milliseconds=1477593000000
let date = NSDate(timeIntervalSince1970: TimeInterval(milliseconds))
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
formatter.locale = NSLocale(localeIdentifier: "en_US") as Locale!
print(formatter.string(from: date as Date))
output:
22-01-48793 01:30:00
Try this,
var date = Date(timeIntervalSince1970: (1477593000000 / 1000.0))
print("date - \(date)")
You will get output as date :
date - 2016-10-27 18:30:00 +0000
How about trying this -
let milisecond = 1479714427
let dateVar = Date.init(timeIntervalSinceNow: TimeInterval(milisecond)/1000)
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy hh:mm"
print(dateFormatter.string(from: dateVar))
Have a look at the documentation of NSDate:
convenience init(timeIntervalSince1970 secs: TimeInterval)
Returns an NSDate object initialized relative to the current date and time by a given number of seconds.
Just convert your milliseconds to seconds and you should get the correct date.

Convert String to Date Returns nil for afternoon times only [duplicate]

This question already has answers here:
NSDateFormatter producing (null) SQLite and iOS 5.1
(2 answers)
Closed 6 years ago.
I have the code to convert a string to a date as:
let dateString = detailData.value["eventStart"] as! String
print(dateString)
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm:ss Z" /*find out and place date format from http://userguide.icu-project.org/formatparse/datetime*/
let date = dateFormatter.dateFromString(dateString) // || date!.timeIntervalSince1970 >= presentDate.timeIntervalSince1970
print(date?.timeIntervalSince1970)
The output is:
2016-05-31 03:18:11 +0000
Optional(1464664691.0)
2016-06-03 14:00:44 +0000
nil
2016-06-01 00:38:08 +0000
Optional(1464741488.0)
2016-06-04 00:25:55 +0000
Optional(1464999955.0)
2016-05-31 22:19:54 +0000
nil
I see that the time in the afternoon (is 12+) return nil.
How can I prevent this?
Your date parsing is failing. In your case, its failing because of an invalid hour component in the input string.
Use 24-hour format HH instead of 12-hour format hh.

Resources