Difference Between 'Yyyy' and 'Yyyy' in Nsdateformatter

Difference between 'YYYY' and 'yyyy' in NSDateFormatter

Also when using a date format string using the correct format is important.

@"YYYY" is week-based calendar year.

@"yyyy" is ordinary calendar year.

You can go through the whole blog, its a good to give it a look

Sample Image

https://web.archive.org/web/20150423093107/http://realmacsoftware.com/blog/working-with-date-and-time

http://realmacsoftware.com/blog/working-with-date-and-time (dead link)

NSDateFormatter returns nil with format YYYY-MM-ddTHH:mm:ssZ

that would be a better formatter.

[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];

NSDateFormatter for datetime

try

 "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"

The Date Format Patterns guide suggests that "S" is the format specifier for fractions of seconds.

Weird behavior of NSDate, NSDateFormatter

My answer for your question is below

"YYYY" is week-based calendar year.

"yyyy" is ordinary calendar year.

For more details

Difference between 'yyyy' and 'YYYY'

Unicode Standard

NSDateFormatter and stringFromDate

You need to refer to UTC timezone

    let dateString = "2016 01 17"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = NSTimeZone(abbreviation: "UTC")
let s = dateFormatter.dateFromString(dateString)
print(s)

Date formatter not changing properly

The “yyyy” represents the calendar year of the date while the “YYYY” represents the year of the week.

for detail information, please see this Difference between 'YYYY' and 'yyyy' in NSDateFormatter

let getInputString = "2021-03-05T12:21:18"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
if let getInputDate = dateFormatter.date(from:getInputString){
dateFormatter.dateFormat = "dd-MM-yyyy"
let getoutputString = dateFormatter.string(from: getInputDate)
}

Note : as per @LeoDabus guidelines please ensure the self.Due_Date is empty or not before start the conversion.

Dateformatter returns different dates

Use below dateFormat instead

df.dateFormat = "MMMM dd, yyyy HH:mm"


Related Topics



Leave a reply



Submit