### 【ISO 8601】時間也會自己跨年！Swift、Python、C# 年份格式比較

• 118
• 0

C#Pythondatetime的相關功能是否也會有相同問題？

3.1.1.23

week calendar

calendar (3.1.1.18) based on an unbounded series of contiguous calendar weeks (3.1.2.16) that uses the time scale unit (3.1.1.7) of calendar week as its basic unit to represent a calendar year (3.1.2.21), according to the rule that the first calendar week of a calendar year is the week including the first Thursday of that year, and that the last one is the week immediately preceding the first calendar week of the next calendar year

Note 1 to entry: This rule is based on the principle that a week belongs to the calendar year to which the majority of its calendar days (3.1.2.11) belong.

Note 2 to entry: In the week calendar, calendar days of the first and last calendar week of a calendar year may belong to the previous and the next calendar year respectively in the Gregorian calendar (3.1.1.19).

Note 3 to entry: The week calendar is described in 4.2.2.

There are several mutually equivalent and compatible descriptions of week 01:

• the week with the year's first Thursday in it (the formal ISO definition),
• the week with 4 January in it,
• the first week with the majority (four or more) of its days in the starting year, and
• the week starting with the Monday in the period 29 December – 4 January.

## Swift

Swift中「YYYY」是以「week」為基準的日曆，所以會是2020；而「yyyy」是一般的日曆，也就會是2019。

let currentdate = Date()
let dateformatter = DateFormatter()
dateformatter.locale = Locale(identifier: "zh_Hant_TW")
dateformatter.dateFormat = "YYYYMMddHHmm"
guard let customDate = Int(dateformatter.string(from: currentdate)) else{
assertionFailure("[AssertionFailure] Date: customDate dateformatter is invaild!! (View02WeatherObservationViewController)")
return
}

A common mistake is to use YYYY. yyyy specifies the calendar year whereas YYYY specifies the year (of “Week of Year”), used in the ISO year-week calendar.

## C#

DateTime dateTime = DateTime.Now;
Console.WriteLine($"formate[DateTime.Now],time=[{DateTime.Now}]"); Console.WriteLine($"formate[DateTime.Now.Year],time=[{DateTime.Now.Year}]");

Console.WriteLine($"formate[yy],time=[{DateTime.Now.ToString("yy")}]"); Console.WriteLine($"formate[YY],time=[{DateTime.Now.ToString("YY")}]");

Console.WriteLine($"formate[yyyy],time=[{DateTime.Now.ToString("yyyy")}]"); Console.WriteLine($"formate[YYYY],time=[{DateTime.Now.ToString("YYYY")}]");

Console.WriteLine(\$"formate[yyyyy],time=[{DateTime.Now.ToString("yyyyy")}]");

formate[DateTime.Now],time=[12/31/2019 4:14:31 PM]
formate[DateTime.Now.Year],time=[2019]
formate[yy],time=[19]
formate[YY],time=[YY]
formate[yyyy],time=[2019]
formate[YYYY],time=[YYYY]
formate[yyyyy],time=[02019]

## Python

import datetime

print_str = 'format[%s],time=[%s]'
time_now = datetime.datetime.now()

print((print_str % ('datetime.now', time_now)))
print((print_str % ('datetime.now.year', time_now.year)))
print((print_str % ('datetime.now.ctime', time_now.ctime())))
print((print_str % ('%Y', time_now.strftime('%Y'))))
print((print_str % ('%y', time_now.strftime('%y'))))


format[datetime.now],time=[2019-12-31 14:32:13.077664]
format[datetime.now.year],time=[2019]
format[datetime.now.ctime],time=[Tue Dec 31 14:32:13 2019]
format[%Y],time=[2019]
format[%y],time=[19]

C#Python則沒有這個問題。