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.
Related
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)
This question already has an answer here:
How to format DateFormatter(NSDateFormattter) for Date String
(1 answer)
Closed 5 years ago.
I having a date string:
Wed Mar 25 2017 05:30:00 GMT+0530 (IST)
I want to convert this string to the Date object in Swift 3.
I have referred to Link1, Link 2 and Link 3 for generating the format for this date string but could not get the correct format.
1) If you have string which contains timezone names like IST or GMT differences, then you can use NSDateDetector as explained in this SO post answer:
extension String {
var nsString: NSString { return self as NSString }
var length: Int { return nsString.length }
var nsRange: NSRange { return NSRange(location: 0, length: length) }
var detectDates: [Date]? {
return try? NSDataDetector(types: NSTextCheckingResult.CheckingType.date.rawValue)
.matches(in: self, range: nsRange)
.flatMap{$0.date}
}
}
//your example here
let dateString = "Wed Mar 25 2017 05:30:00 GMT+0530 (IST)"
if let dateDetected = dateString.detectDates?.first {
let date = dateDetected//Mar 25, 2017, 5:30 AM
print(dateDetected)//2017-03-25 00:00:00 +0000 - this is GMT time
}
Mar 25, 2017, 5:30 AM //date converted to local time zone
2017-03-25 00:00:00 +0000 //printed value of GMT time
2) Or if you some how able to remove GMT and IST reference from your string then try this:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEE, MMM d yyyy HH:mm:ss Z"
let date = dateFormatter.date(from: "Wed Mar 25 2017 05:30:00 +0530")!
print(date)
It will give you
Mar 25, 2017, 5:30 AM //date converted to local time zone
2017-03-25 00:00:00 +0000 //printed value of GMT time
var dateString = "Wed, 25 Mar 2017 05:30:00 +0000"
var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "E, d MMM yyyy HH:mm:ss Z"
var dateFromString = dateFormatter.date(from: dateString)
print(dateFromString)
This question already has answers here:
How can I convert string date to NSDate?
(18 answers)
Closed 5 years ago.
I want to convert December 20, 2016 to 12/20/16.
December 20 is in String format. Not too sure if I can use NSDateFormatter for this. I do not what MM/DD/YYYY to something like YYYY:DD:MM. I want to convert a String December 20, 2016, and get it to 12/20/16.
Yes you can use DateFormatter (without NS in Swift 3, the same applies for Date not NSDate). To properly parse your date string you need to use the date format "MMMM dd, yyyy" and set the date formatter locale to "en_US_POSIX"
let string = "December 20, 2016"
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = "MMMM dd, yyyy"
if let date = formatter.date(from: dateString) {
print(date) // "2016-12-20 02:00:00 +0000\n"
formatter.dateStyle = .short
let stringFromDate = formatter.string(from: date) // "12/20/16"
}
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"
I want to get 2018-09-28 from 2018-09-28 09:42:00 +0000 in Date format. I
can extract the same in string format but I want to get this in Date format. Here is my sample code.
let date = Date(timeIntervalSince1970: (TimeInterval(timer/1000)))
let df = DateFormatter()
df.dateFormat = "yyyy-MM-dd"
let myDate = df.string(from: date)
let updateDate = df.date(from: myDate)
//date - 2018-09-28
//updateDate - 2018-09-28 09:42:00 +0000
You can simply get your date string prefix 11 and insert noon time when parsing your string:
let str = "2018-09-28 09:42:00 +0000"
let df = DateFormatter()
df.locale = Locale(identifier: "en_US_POSIX")
df.dateFormat = "yyyy-MM-dd HH:mm"
if let date = df.date(from: str.prefix(11) + "12:00") {
print(date.description(with: .current))
}
// Friday, September 28, 2018 at 12:00:00 PM Brasilia Standard Time