This question already has answers here:
Difference between 'YYYY' and 'yyyy' in NSDateFormatter
(4 answers)
Closed 2 years ago.
Please check the code bellow:
func testDate() {
let calendar = Calendar.current
let dateEnd = calendar.date(byAdding: .day, value: 15, to: Date())
let df:DateFormatter = DateFormatter()
df.dateFormat = "MMMM dd, YYYY HH:mm"
df.timeZone = calendar.timeZone
df.timeZone = Calendar.current.timeZone
let startDateLabel = df.string(from: dateEnd!)
print(startDateLabel)
let min = df.date(from: startDateLabel)!
print(min)
}
Output
print1: March 12, 2020 14:48
print2: 2019-12-22 09:48:00 +0000
Use below dateFormat instead
df.dateFormat = "MMMM dd, yyyy HH:mm"
printing the date to console would give you the print2. It's printing the Date formats debug description. If you require a particular format you need to convert date into string with a DateFormatter. Check this link you need more help to get the desired date format.
Related
This question already has an answer here:
Swift ISO8601 format to Date returning fatal error
(1 answer)
Closed 1 year ago.
I think this is an ISO8601 formatted timestamp.
2021-07-05T22:26:51.159Z
I'm trying to convert it with ISO8601DateFormatter() in swift 5.
Here's what I've tried:
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = .withFullDate
//ISO8601DateFormatter().formatOptions = .withFractionalSeconds
let d = "2021-07-05T22:26:51.159Z"
let date = dateFormatter.date(from: d)
The result:
date = 2021-07-05 00:00:00 UTC
The day is correct, the time is not. I've tried to set the .withFractionalSeconds option. Didn't help.
How should I convert this format?
You can use standard date formatter to achieve this:
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
let date = dateFormatter.date(from: d)
print(date)
This question already has answers here:
How can I parse / create a date time stamp formatted with fractional seconds UTC timezone (ISO 8601, RFC 3339) in Swift?
(13 answers)
Closed 3 years ago.
I'm formatting a date string to date object using below code. But for some date strings it works and for some it returns nil.
Format = "yyyy-MM-dd'T'HH:mm:ss.sssZ"
"2019-04-02T09:47:24.055Z" Works
"2019-03-27T22:31:17.140Z" Doesn't work
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.sssZ"
let date = dateFormatter.date(from: "2019-03-27T22:31:17.140Z")
Your format is wrong. It should be:
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
Have a look at date time format here:
https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let date = dateFormatter.date(from: "2019-04-02T09:47:24.055Z")
let date1 = dateFormatter.date(from: "2019-03-27T22:31:17.140Z")
print(">>>>>!!", date, date1)
Optional(2019-04-02 09:47:24 +0000) Optional(2019-03-27 22:31:17 +0000)
The code above works as expected.
use SSS instead of sss
for your case of
yyyy-MM-dd'T'HH:mm:ss.sssZ and 2019-04-02T09:47:24.055Z
output was :
Optional(2019-04-02 09:47:55 +0000) nil
look at minutes and seconds:)
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 answers here:
NSDateFormatter- stringFromDate not returning AM/PM
(6 answers)
Closed 5 years ago.
To convert date and time, I have used this:
let formatter = NSDateFormatter()
formatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
formatter.dateFormat = "M/dd/yyyy, h:mm"
formatter.AMSymbol = "AM"
formatter.PMSymbol = "PM"
let dateString = formatter.stringFromDate(model.courseDate)
print(dateString)
If the course date is "courseDate = "2017-01-09 01:00:00 +0000";"
but I am getting like this in print:
1/09/2017, 1:00 with no any AM or PM.
How can I achieve this?
Thanks.
according to the date formatter specs
You can retrieve a date in 24h format instead of requesting AM/PM by using
HH instead of hh
so the date "21/11/2016 01:30" (pm) would be "21/11/2016 13:30".
if you do need to print the format, though, you can print it using:
a
More date fomatting details here
As per Apple general date formatter you should use "M/dd/yyyy, hh:mm a" in your date format.
like : formatter.dateFormat = "M/dd/yyyy, hh:mm a"
Hope this will help you!
use following code may help full for you
//Call function
let dateTime = self.convertDateFormaterAamer(sendTimeStep, inputDateFormate: "dd-MM-yyyy HH:mm:ss", outputDateFormate: "dd-MMM-yyyy hh:mm a")
//Date function
func convertDateFormater(date: String,inputDateFormate: String , outputDateFormate: String) -> String
{
let dateFormatter = NSDateFormatter()
//dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
//dateFormatter.timeZone = NSTimeZone(name: "UTC")
dateFormatter.dateFormat = inputDateFormate
let date = dateFormatter.dateFromString(date)
//dateFormatter.dateFormat = "yyyy MMM EEEE HH:mm"
//dateFormatter.timeZone = NSTimeZone(name: "UTC")
dateFormatter.dateFormat = outputDateFormate
var timeStamp = ""
if (date != nil)
{
timeStamp = dateFormatter.stringFromDate(date!)
}
return timeStamp
}
This question already has answers here:
NSDateFormatter return incorrect date from string
(3 answers)
Closed 7 years ago.
I'm trying to convert a Date and Time String to NSDate with Swift, But the problem is The time is not converting perfectly, Even i set my Timezone correctly, Here's my code:
var dateStr = "2015-10-16 08:00 AM"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 28800)
var date = dateFormatter.dateFromString(dateStr)
print(date!)
And it prints out like this: 2015-10-16 00:00:00 +0000 What do you think seems to be the problem? Thanks!
Nothing wrong here. You should convert date to String before print it.
let dateStr = "2015-10-16 08:00 AM"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 28800)
let date = dateFormatter.dateFromString(dateStr)
print(dateFormatter.stringFromDate(date!))
or as in Swift 3
let dateStr = "2015-10-16 08:00 AM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd hh:mm a"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 28800)
let date = dateFormatter.date(from: dateString)
print(dateFormatter.string(from: date))
The same result: 2015-10-16 08:00 AM