最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift格式化日期时间为字符串的完整的格式的语法

Swift crifan 3565浏览 0评论

之前就知道,用于格式化NSDate的字符串,有:

“yyyy/MM/dd HH:mm”

但是此处,想要格式化获得:

月份是1个数字的那种

-》超过9月份的得到的分别是两位的:10,11,12

好像应该是:M

-》”M/dd HH:mm”

但是不确定。

-》更加想要知道:

完整的,日期的格式化的语法

swift dateFormat

参考:

Swiftly getting a human-readable date with NSDateFormatter

别人总结了。

[总结]

关于日期时间的格式化成字符串的语法,对应的格式说明:

官网:

UTS #35: Unicode LDML: Dates

day

FieldSym.No.ExampleDescription
eraG1..3ADEra – Replaced with the Era string for the current date. One to three letters for the abbreviated form, four letters for the long form, five for the narrow form.
4Anno Domini
5A
yeary1..n1996
Year. Normally the length specifies the padding, but for two letters it also specifies the maximum length. Example:
Yearyyyyyyyyyyyyyyy
AD 1101001000100001
AD 121212012001200012
AD 12312323123012300123
AD 12341234341234123401234
AD 123451234545123451234512345

Y1..n1997Year (in “Week of Year” based calendars). Normally the length specifies the padding, but for two letters it also specifies the maximum length. This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.
u1..n4601Extended year. This is a single number designating the year of this calendar system, encompassing all supra-year fields. For example, for the Julian calendar system, year numbers are positive, with an era of BCE or CE. An extended year value for the Julian calendar system assigns positive values to CE years and negative values to BCE years, with 1 BCE being year 0.
U1..3甲子Cyclic year name. Calendars such as the Chinese lunar calendar (and related calendars) and the Hindu calendars use 60-year cycles of year names. Use one through three letters for the abbreviated name, four for the full name, or five for the narrow name (currently the data only provides abbreviated names, which will be used for all requested name widths). If the calendar does not provide cyclic year name data, or if the year value to be formatted is out of the range of years for which cyclic name data is provided, then numeric formatting is used (behaves like ‘y’).
4(currently also 甲子)
5(currently also 甲子)
quarterQ1..202Quarter – Use one or two for the numerical quarter, three for the abbreviation, or four for the full name.
3Q2
42nd quarter
q1..202Stand-Alone Quarter – Use one or two for the numerical quarter, three for the abbreviation, or four for the full name.
3Q2
42nd quarter
monthM1..209Month – Use one or two for the numerical month, three for the abbreviation, four for the full name, or five for the narrow name.
3Sept
4September
5S
L1..209Stand-Alone Month – Use one or two for the numerical month, three for the abbreviation, or four for the full name, or 5 for the narrow name.
3Sept
4September
5S
l1(nothing)This pattern character is deprecated, and should be ignored in patterns. It was originally intended to be used in combination with M to indicate placement of the symbol for leap month in the Chinese calendar. Placement of that marker is now specified using locale-specific <monthPatterns> data, and formatting and parsing of that marker should be handled as part of supporting the regular M and L pattern characters.
weekw1..227Week of Year.
W13Week of Month
dayd1..21Date – Day of the month
D1..3345Day of year
F12Day of Week in Month. The example is for the 2nd Wed in July
g1..n2451334Modified Julian day. This is different from the conventional Julian day number in two regards. First, it demarcates days at local zone midnight, rather than noon GMT. Second, it is a local number; that is, it depends on the local time zone. It can be thought of as a single number that encompasses all the date-related fields.
weekE1..3TuesDay of week – Use one through three letters for the short day, or four for the full name, five for the narrow name, or six for the short name.
4Tuesday
5T
6Tu
e1..22Local day of week. Same as E except adds a numeric value that will depend on the local starting day of the week, using one or two letters. For this example, Monday is the first day of the week.
3Tues
4Tuesday
5T
6Tu
c12Stand-Alone local day of week – Use one letter for the local numeric value (same as ‘e’), three for the short day, four for the full name, five for the narrow name, or six for the short name. 
3Tues
4Tuesday
5T
6Tu
perioda1AMAM or PM
hourh1..211Hour [1-12]. When used in skeleton data or in a skeleton passed in an API for flexible date pattern generation, it should match the 12-hour-cycle format preferred by the locale (h or K); it should not match a 24-hour-cycle format (H or k). Use hh for zero padding.
H1..213Hour [0-23]. When used in skeleton data or in a skeleton passed in an API for flexible date pattern generation, it should match the 24-hour-cycle format preferred by the locale (H or k); it should not match a 12-hour-cycle format (h or K). Use HH for zero padding.
K1..20Hour [0-11]. When used in a skeleton, only matches K or h, see above. Use KK for zero padding.
k1..224Hour [1-24]. When used in a skeleton, only matches k or H, see above. Use kk for zero padding.
j1..2n/aThis is a special-purpose symbol. It must not occur in pattern or skeleton data. Instead, it is reserved for use in skeletons passed to APIs doing flexible date pattern generation. In such a context, it requests the preferred hour format for the locale (h, H, K, or k), as determined by whether h, H, K, or k is used in the standard short time format for the locale. In the implementation of such an API, ‘j’ must be replaced by h, H, K, or k before beginning a match against availableFormats data. Note that use of ‘j’ in a skeleton passed to an API is the only way to have a skeleton request a locale’s preferred time cycle type (12-hour or 24-hour).
minutem1..259Minute. Use one or two for zero padding.
seconds1..212Second. Use one or two for zero padding.
S1..n3456Fractional Second – truncates (like other time fields) to the count of letters. (example shows display using pattern SSSS for seconds value 12.34567)
A1..n69540000Milliseconds in day. This field behaves exactly like a composite of all time-related fields, not including the zone fields. As such, it also reflects discontinuities of those fields on DST transition days. On a day of DST onset, it will jump forward. On a day of DST cessation, it will jump backward. This reflects the fact that is must be combined with the offset field to obtain a unique local time value.
zonez1..3PDTThe short specific non-location format. Where that is unavailable, falls back to the short localized GMT format(“O”).
4Pacific Daylight TimeThe long specific non-location format. Where that is unavailable, falls back to the long localized GMT format(“OOOO”).
Z1..3-0800The ISO8601 basic format with hours, minutes and optional seconds fields. The format is equivalent to RFC 822 zone format (when optional seconds field is absent). This is equivalent to the “xxxx” specifier.
4GMT-8:00The long localized GMT format. This is equivalent to the “OOOO” specifier.
5

-08:00

-07:52:58

The ISO8601 extended format with hours, minutes and optional seconds fields. The ISO8601 UTC indicator “Z” is used when local time offset is 0. This is equivalent to the “XXXXX” specifier.
O1GMT-8The short localized GMT format.
4GMT-08:00The long localized GMT format.
v1PTThe short generic non-location format. Where that is unavailable, falls back to the generic location format (“VVVV”), then the short localized GMT format as the final fallback.
4Pacific TimeThe long generic non-location format. Where that is unavailable, falls back to generic location format (“VVVV”).
V1uslax

The short time zone ID. Where that is unavailable, the special short time zone ID unk (Unknown Zone) is used.

Note: This specifier was originally used for a variant of the short specific non-location format, but it was deprecated in the later version of this specification. In CLDR 23, the definition of the specifier was changed to designate a short time zone ID.

2America/Los_AngelesThe long time zone ID.
3Los AngelesThe exemplar city (location) for the time zone. Where that is unavailable, the localized exemplar city name for the special zone Etc/Unknown is used as the fallback (for example, “Unknown City”). 
4Los Angeles Time

The generic location format. Where that is unavailable, falls back to the long localized GMT format (“OOOO”; Note: Fallback is only necessary with a GMT-style Time Zone ID, like Etc/GMT-830.)

This is especially useful when presenting possible timezone choices for user selection, since the naming is more uniform than the “v” format.

X1

-08

+0530

Z

The ISO8601 basic format with hours field and optional minutes field. The ISO8601 UTC indicator “Z” is used when local time offset is 0. (The same as x, plus “Z”.)
2

-0800

Z

The ISO8601 basic format with hours and minutes fields. The ISO8601 UTC indicator “Z” is used when local time offset is 0. (The same as xx, plus “Z”.)
3

-08:00

Z

The ISO8601 extended format with hours and minutes fields. The ISO8601 UTC indicator “Z” is used when local time offset is 0. (The same as xxx, plus “Z”.)
4

-0800

-075258

Z

The ISO8601 basic format with hours, minutes and optional seconds fields. The ISO8601 UTC indicator “Z” is used when local time offset is 0. (The same as xxxx, plus “Z”.)

Note: The seconds field is not supported by the ISO8601 specification.

5

-08:00

-07:52:58

Z

The ISO8601 extended format with hours, minutes and optional seconds fields. The ISO8601 UTC indicator “Z” is used when local time offset is 0. (The same as xxxxx, plus “Z”.)

Note: The seconds field is not supported by the ISO8601 specification.

x1

-08

+0530

The ISO8601 basic format with hours field and optional minutes field. (The same as X, minus “Z”.)
2-0800The ISO8601 basic format with hours and minutes fields. (The same as XX, minus “Z”.)
3-08:00The ISO8601 extended format with hours and minutes fields. (The same as XXX, minus “Z”.)
4

-0800

-075258

The ISO8601 basic format with hours, minutes and optional seconds fields. (The same as XXXX, minus “Z”.)

Note: The seconds field is not supported by the ISO8601 specification.

5

-08:00

-07:52:58

The ISO8601 extended format with hours, minutes and optional seconds fields. (The same as XXXXX, minus “Z”.)

Note: The seconds field is not supported by the ISO8601 specification.

别人:

Swiftly getting a human-readable date with NSDateFormatter

总结的,更容易看懂:

FormatDescriptionExample
“y”year with at least 1 digit.
1 AD →        “1”
42 AD →     “42”
2014 AD → “2014”
“yy”
year with exactly 2 digits.
If less, it is padded with a zero.
It will be truncated to the tens digit if larger.
1 AD → “01”
42 AD → “42”
2014 AD → “14”
“yyy”
year with at least 3 digits.
If less, it is padded with zeros.
1 AD →  “001”
42 AD →  “042”
2014 AD → “2014”
“yyyy”
year with at least 3 digits.
If less, it is padded with zeros.
1 AD → “0001”
42 AD → “0042”
2014 AD → “2014”
“M”month with at least 1 digit.
July → “7”
December → “12”
“MM”
month with at least 2 digits.
If less, it is padded with zeros.
July → “07”
December → “12”
“MMM”Three letter month abbreviation.
July → “Jul”
December → “Dec”
“MMMM”Full name of month.
July → “July”
December → “December”
“MMMMM”
One letter month abbreviation.
Not unique, January, June, and July are all “J”.
July → “J”
December → “D”
“d”day with at least 1 digit.
4 → “4”
25 → “25”
“dd”
day with at least 2 digits.
If less, it is padded with a zero.
4 → “04”
25 → “25”
“E”, “EE”, or”EEE”3 letter day abbreviation of day name.
Wednesday → “Wed”
Thursday → “Thu”
“EEEE”Full day name.
Wednesday → “Wednesday”
Thursday → “Thursday”
“EEEEE”
1 letter day abbreviation of day name.
Not unique, Tuesday and Thursday are both “T”.
Wednesday → “W”
Thursday → “T”
“EEEEEE”2 letter day abbreviation of day name.
Wednesday → “We”
Thursday → “Th”
“a”Period of day (AM/PM).
5 PM → “PM”
7 AM → “AM”
“h”A 1-12 based hour with at least 1 digit.
5 PM → “5”
7 AM → “7”
“hh”
A 1-12 based hour with at least 2 digits.
If less, it is padded with a zero.
5 PM → “05”
7 AM → “07”
“H”A 0-23 based hour with at least 1 digit.
5 PM → “17”
7 AM → “7”
“HH”
A 0-23 based hour with at least 2 digits.
If less, it is padded with a zero.
5 PM → “17”
7 AM → “07”
“m”minute with at least 1 digit.
40 → “40”
1 → “1”
“mm”
minute with at least 2 digits.
If less, it is padded with a zero.
40 → “40”
1 → “01”
“s”second with at least 1 digit.
40 → “40”
1 → “1”
“ss”
second with at least 2 digits.
If less, it is padded with a zero.
40 → “40”
1 → “01”
“S”Tenths place of fractional second.
123 ms → “1”
7 ms → “0”
“SS”
Tenths and hundredths
place of a fractional second.
123 ms → “12”
7 ms → “00”
“SSS”
Tenths, hundredths, and thousandths
place of a fractional second.
123 ms → “123”
7 ms → “007”

举例:

“yyyy/MM/dd HH:mm:ss” -》2016/06/02 20:28:03

“M/dd HH:mm” -》6/02 20:28

[后记 20160825]

后来在折腾:

[已解决]Python中用strftime格式化datetime出错

的时候,发现Python中关于日期时间的格式化的语法,和此处的通用不太一样

主要是:

Python中是:

%M=minute=分钟

%m=month=月份

而其它通用的语法是:

%M=month=月份

%m=minute=分钟

此处附上,Python中处理datetime,date,time的strptime,strftime所用到的格式化的语法:

8.1. datetime — Basic date and time types — Python 2.7.12 documentation

DirectiveMeaningExampleNotes
%aWeekday as locale’s abbreviated name.
Sun, Mon, …, Sat (en_US);
So, Mo, …, Sa (de_DE)
(1)
%AWeekday as locale’s full name.
Sunday, Monday, …, Saturday (en_US);
Sonntag, Montag, …, Samstag (de_DE)
(1)
%wWeekday as a decimal number, where 0 is Sunday and 6 is Saturday.0, 1, …, 6 
%dDay of the month as a zero-padded decimal number.01, 02, …, 31 
%bMonth as locale’s abbreviated name.
Jan, Feb, …, Dec (en_US);
Jan, Feb, …, Dez (de_DE)
(1)
%BMonth as locale’s full name.
January, February, …, December (en_US);
Januar, Februar, …, Dezember (de_DE)
(1)
%mMonth as a zero-padded decimal number.01, 02, …, 12 
%yYear without century as a zero-padded decimal number.00, 01, …, 99 
%YYear with century as a decimal number.1970, 1988, 2001, 2013 
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, …, 23 
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, …, 12 
%pLocale’s equivalent of either AM or PM.
AM, PM (en_US);
am, pm (de_DE)
(1), (2)
%MMinute as a zero-padded decimal number.00, 01, …, 59 
%SSecond as a zero-padded decimal number.00, 01, …, 59(3)
%fMicrosecond as a decimal number, zero-padded on the left.000000, 000001, …, 999999(4)
%zUTC offset in the form +HHMM or -HHMM (empty string if the the object is naive).(empty), +0000, -0400, +1030(5)
%ZTime zone name (empty string if the object is naive).(empty), UTC, EST, CST 
%jDay of the year as a zero-padded decimal number.001, 002, …, 366 
%UWeek number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0.00, 01, …, 53(6)
%WWeek number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0.00, 01, …, 53(6)
%cLocale’s appropriate date and time representation.
Tue Aug 16 21:30:00 1988 (en_US);
Di 16 Aug 21:30:00 1988 (de_DE)
(1)
%xLocale’s appropriate date representation.
08/16/88 (None);
08/16/1988 (en_US);
16.08.1988 (de_DE)
(1)
%XLocale’s appropriate time representation.
21:30:00 (en_US);
21:30:00 (de_DE)
(1)
%%A literal ‘%’ character.% 

转载请注明:在路上 » [已解决]swift格式化日期时间为字符串的完整的格式的语法

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.205 seconds, using 22.23MB memory