欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

date-fns parse 有大用

解析


描述

使用给定的格式字符串返回从字符串解析的日期。

包装在两个单引号字符(')之间的格式字符串中的字符被转义。 一行中的两个单引号,无论是在引号序列的内部还是外部,都代表“真实”单引号。

格式字符串的格式基于Unicode技术标准#35:https//www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table,其中有一些补充(请参见表格下方的注释5)。

可接受的格式字符串模式:

单元图案结果示例笔记
时代140G..GGG公元前


GGGG安诺·多米尼(Anno Domini),在基督之前2


GGGGGA,B
公历年130ÿ44,1,1900,2017,99994


第44、1、1900、99999994,5


YY44,01,00,174


YYY044、001、123、9994


YYYY0044、0001、1900、20174


YYYYY...2,4
当地星期编号年130ÿ44,1,1900,2017,90004


第44、1、1900、99999994,5


YY44,01,00,174


YYY044、001、123、9994


YYYY0044、0001、1900、20174


YYYYY...2,4
ISO周编号年份130[R-43、1、1900、2017、9999,-99994,5


RR-43,01,00,174,5


存款准备金率-043、001、123、999,-9994,5


RRRR-0043、0001、2017、9999,-99994,5


RRRRR...2,4,5
延长一年130ü-43、1、1900、2017、9999,-9994


UU-43、01、99,-994


UUU-043、001、123、999,-9994


UUUU-0043、0001、2017、9999,-99994


UUUUU...2,4
季度(格式)120Q1 2 3 4


QO1、2、3、4


QQ01、02、03、04


QQQQ1,Q2,Q3,Q4


QQQQ第一季度,第二季度,...2


QQQQQ1 2 3 44
季度(独立)120q1 2 3 4


QO1、2、3、4


QQ01、02、03、04


QQQQ1,Q2,Q3,Q4


QQQQ第一季度,第二季度,...2


QQQQQ1 2 3 43
月(格式)110中号1,2,...,12


1、2,...,12


MM01、02,...,12


MMM一月,二月,...,十二月


MMMM一月,二月,...,十二月2


MMMMMJ,F,...,D
月(独立)110大号1,2,...,12


1、2,...,12


01、02,...,12


LLL一月,二月,...,十二月


LLLL一月,二月,...,十二月2


LLLLLJ,F,...,D
一年中的当地一周100w ^1,2,...,53


WO1、2,...,53


WW01,02,...,53
一年中的ISO周100一世1,2,...,53


木卫一1、2,...,53


II01,02,...,53
一个月中的某天90d1,2,...,31


1st,2nd,...,31st


DD01,02,...,31
一年中的一天90d1,2,...,365,366


1、2,...,365、366


DD01、02,...,365、366


DDD001,002,...,365,366


DDDD...2
星期几(格式化)90E..EEE周一,周二,周三,...,苏


EEEE星期一,星期二,...,星期日2


EEEEEM,T,W,T,F,S,S


EEEEEEMo,Tu,We,Th,Fr,Su,Sa
ISO星期几(格式)90一世1,2,3,...,7


IO第一,第二,...,第七


01,02,...,07


周一,周二,周三,...,苏


IIII星期一,星期二,...,星期日2,5


IIIIIM,T,W,T,F,S,S


IIIIIIMo,Tu,We,Th,Fr,Su,Sa
当地星期几(格式)90Ë2,3,4,...,1


EO第二,第三,...,第一


EE02,03,...,01


EEE周一,周二,周三,...,苏


EEEE星期一,星期二,...,星期日2


EEEEEM,T,W,T,F,S,S


EEEEEEMo,Tu,We,Th,Fr,Su,Sa
本地星期几(独立)90C2,3,4,...,1


合作第二,第三,...,第一


CC02,03,...,01


CCC周一,周二,周三,...,苏


CCCC星期一,星期二,...,星期日2


CCCCCM,T,W,T,F,S,S


CCCCCCMo,Tu,We,Th,Fr,Su,Sa
上午下午80a..aaa上午下午


AAAA上午下午2


AAAAA
上午,下午,中午,午夜80b..bbb上午,下午,中午,午夜


BBBB上午,下午,中午,午夜2


BBBBBa,p,n,mi
弹性时段80B..BBB晚上,早晨,...


BBBB晚上,早晨,...2


BBBBB晚上,早晨,...
时间[1-12]70H1,2,...,11,12


1、2,...,11、12


HH01、02,...,11、12
小时[0-23]70H0,1,2,...,23


0th,1st,2nd,...,23rd


HH00、01、02,...,23
小时[0-11]70ķ1,2,...,11,0


1、2,...,11、0


KK1,2,...,11,0
时间[1-24]70ķ24,1,2,...,23


KO24th,1st,2nd,...,23rd


KK24、01、02,...,23
分钟600,1,...,59


0th,1st,...,59th


毫米00、01,...,59
第二50小号0,1,...,59


所以0th,1st,...,59th


SS00、01,...,59
秒的小数40小号0,1,...,9


SS00、01,...,99


SSS000、0001,...,999


SSSS...2
时区(ISO-8601 w / Z)20X-08,+ 0530,Z


XX-0800,+ 0530,Z


XXX-08:00,+ 05:30,Z


XXXX-0800,+ 0530,Z,+ 1234562


XXXXX-08:00,+ 05:30,Z,+ 12:34:56
时区(ISO-8601,不含Z)20X-08,+ 0530,+ 00


XX-0800,+ 0530,+ 0000


XXX-08:00,+ 05:30,+ 00:002


XXXX-0800,+ 0530,+ 0000,+ 123456


XXXXX-08:00,+ 05:30,+ 00:00,+ 12:34:56
秒时间戳10Ť512969520


TT...2
毫秒时间戳10Ť512969520900


TT...2

笔记:

  1. 在默认的美国语言环境 中,“格式化”单位(例如,格式化季度)与“独立”单位相同,但在某些语言中有所不同。在日期的上下文中 ,根据语言规则拒绝“格式化”单位 “独立”单位始终是主格单数。 功能上,它们将产生不同的结果:format

    format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'

    format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'

    parse 将尝试可互换地匹配格式和独立单元。

  2. 相同字母的任何序列都是一个模式,除非由 单引号引起来(请参见下文)。 如果序列长于表中所列:

  • 对于数字单位(将尝试匹配与序列一样宽的数字yyyyyyyyparse

  • 对于文本单位(将尝试匹配该单位的最大变体形式。这些变化在表的最后一栏中标记为“ 2”。MMMMMMMMparse

  • QQQQQ并且在某些地区可能不是严格的数字形式。这些令牌表示该季度的最短形式。qqqqq

  • 模式之间的主要区别是BC年:yu

    yu
    AC 111
    公元前1年10
    公元前2年2-1

    将尝试通过接近来猜测两位数的世纪yybaseDate

    parse('50', 'yy', new Date(2018, 0, 1)) //=> Sat Jan 01 2050 00:00:00

    parse('75', 'yy', new Date(2018, 0, 1)) //=> Wed Jan 01 1975 00:00:00

    只是按原样分配年份:uu

    parse('50', 'uu', new Date(2018, 0, 1)) //=> Sat Jan 01 0050 00:00:00

    parse('75', 'uu', new Date(2018, 0, 1)) //=> Tue Jan 01 0075 00:00:00

    对于本地和ISO周编号年份(),存在相同的差异除了本地周编号年份取决于(比较setISOWeekYearsetWeekYear)。YR options.weekStartsOn options.firstWeekContainsDate

  • 这些模式不在Unicode技术标准#35中:

    • i:ISO星期几

    • I:一年中的ISO周

    • R:ISO周编号年份

    • o:序数修饰符

    值将按照其单位优先级的降序分配给日期。 优先级相同的单元按出现顺序相互覆盖。

    如果没有解析更高优先级的值(例如,解析字符串'January 1st'而没有年份),则 这些值将取自第3个参数,该参数用作解析的上下文。baseDate

    baseDate必须传递该函数才能正常工作。 如果不确定要提供哪个,请创建一个新的Date实例:在这种情况下,将在当前日期的上下文中进行解析。如果或者值转换为有效的那么将被退回。baseDate parse('02/11/2014', 'MM/dd/yyyy', new Date()) baseDateInvalid DateDate Invalid Date

    结果可能因地区而异。

    如果与匹配但不提供令牌,将返回。formatStringdateStringbaseDate

    如果解析失败,将返回。无效日期是一个日期,其时间值为NaN。日期的时间值:http//es5.github.io/#x15.9.1.1Invalid Date

    用法

    var  parse  =  require'date-fns / parse'

    句法

    解析dateStringformatStringbaseDate,[ 选项 ])

    参数

    名称类型描述
    dateString

    the string to parse

    formatString

    the string of tokens

    baseDate日期| 字串| 

    定义解析的dateString中缺少的值

    选项
    (可选的)
    选项

    带选项的对象。查看选项

    属性:
    名称类型描述
    additionalDigits
    (可选,默认为2)
    0 | 1 | 2

    传递给toDatetoDate

    现场
    (可选,默认= defaultLocale)
    语言环境

    语言环境对象。查看语言环境

    weekStartsOn
    (可选,默认= 0)
    0 | 1 | 2 | 3 | 4 | 5 | 6

    一周中第一天的索引(0-星期日)

    firstWeekContainsDate
    (可选,默认为= 1)
    1 | 2 | 3 | 4 | 5 | 6 | 7

    一月的一天,总是在一年的第一周

    返回

    类型描述
    日期

    the parsed date

    例外

    类型描述
    类型错误

    3 arguments required

    引发RangeError

    options.additionalDigits 必须为0、1或2

    引发RangeError

    options.weekStartsOn 必须在0到6之间

    引发RangeError

    options.firstWeekContainsDate 必须介于1到7之间

    引发RangeError

    options.locale必须包含财产match

    例子

    //从2014年2月11日以中端格式解析:
    var  result  =  parse
      '02 / 11/2014'
      'MM / dd / yyyy'
       日期()
    // => 2014年2月11日星期二00:00:00
    //在2010年的背景下,在世界语区域解析2月28日:
     'date-fns / locale / eo' 导入eo  
    var  result  =  parse
      '28 -a de februaro'
      “做'de'MMMM”
      新的 日期201001),
      { localeeo }
    // =>星期日2010年2月28日00:00:00





    来自  https://date-fns.org/v2.0.0-alpha.18/docs/parse


    parse

    Description

    Return the date parsed from string using the given format string.

    The characters in the format string wrapped between two single quotes characters (') are escaped.Two single quotes in a row, whether inside or outside a quoted sequence, represent a 'real' single quote.

    Format of the format string is based on Unicode Technical Standard #35:https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table with a few additions (see note 5 below the table).

    Accepted format string patterns:

    UnitPriorPatternResult examplesNotes
    Era140G..GGGAD, BC


    GGGGAnno Domini, Before Christ2


    GGGGGA, B
    Calendar year130y44, 1, 1900, 2017, 99994


    yo44th, 1st, 1900th, 9999999th4,5


    yy44, 01, 00, 174


    yyy044, 001, 123, 9994


    yyyy0044, 0001, 1900, 20174


    yyyyy...2,4
    Local week-numbering year130Y44, 1, 1900, 2017, 90004


    Yo44th, 1st, 1900th, 9999999th4,5


    YY44, 01, 00, 174


    YYY044, 001, 123, 9994


    YYYY0044, 0001, 1900, 20174


    YYYYY...2,4
    ISO week-numbering year130R-43, 1, 1900, 2017, 9999, -99994,5


    RR-43, 01, 00, 174,5


    RRR-043, 001, 123, 999, -9994,5


    RRRR-0043, 0001, 2017, 9999, -99994,5


    RRRRR...2,4,5
    Extended year130u-43, 1, 1900, 2017, 9999, -9994


    uu-43, 01, 99, -994


    uuu-043, 001, 123, 999, -9994


    uuuu-0043, 0001, 2017, 9999, -99994


    uuuuu...2,4
    Quarter (formatting)120Q1, 2, 3, 4


    Qo1st, 2nd, 3rd, 4th5


    QQ01, 02, 03, 04


    QQQQ1, Q2, Q3, Q4


    QQQQ1st quarter, 2nd quarter, ...2


    QQQQQ1, 2, 3, 44
    Quarter (stand-alone)120q1, 2, 3, 4


    qo1st, 2nd, 3rd, 4th5


    qq01, 02, 03, 04


    qqqQ1, Q2, Q3, Q4


    qqqq1st quarter, 2nd quarter, ...2


    qqqqq1, 2, 3, 43
    Month (formatting)110M1, 2, ..., 12


    Mo1st, 2nd, ..., 12th5


    MM01, 02, ..., 12


    MMMJan, Feb, ..., Dec


    MMMMJanuary, February, ..., December2


    MMMMMJ, F, ..., D
    Month (stand-alone)110L1, 2, ..., 12


    Lo1st, 2nd, ..., 12th5


    LL01, 02, ..., 12


    LLLJan, Feb, ..., Dec


    LLLLJanuary, February, ..., December2


    LLLLLJ, F, ..., D
    Local week of year100w1, 2, ..., 53


    wo1st, 2nd, ..., 53th5


    ww01, 02, ..., 53
    ISO week of year100I1, 2, ..., 535


    Io1st, 2nd, ..., 53th5


    II01, 02, ..., 535
    Day of month90d1, 2, ..., 31


    do1st, 2nd, ..., 31st5


    dd01, 02, ..., 31
    Day of year90D1, 2, ..., 365, 366


    Do1st, 2nd, ..., 365th, 366th5


    DD01, 02, ..., 365, 366


    DDD001, 002, ..., 365, 366


    DDDD...2
    Day of week (formatting)90E..EEEMon, Tue, Wed, ..., Su


    EEEEMonday, Tuesday, ..., Sunday2


    EEEEEM, T, W, T, F, S, S


    EEEEEEMo, Tu, We, Th, Fr, Su, Sa
    ISO day of week (formatting)90i1, 2, 3, ..., 75


    io1st, 2nd, ..., 7th5


    ii01, 02, ..., 075


    iiiMon, Tue, Wed, ..., Su5


    iiiiMonday, Tuesday, ..., Sunday2,5


    iiiiiM, T, W, T, F, S, S5


    iiiiiiMo, Tu, We, Th, Fr, Su, Sa5
    Local day of week (formatting)90e2, 3, 4, ..., 1


    eo2nd, 3rd, ..., 1st5


    ee02, 03, ..., 01


    eeeMon, Tue, Wed, ..., Su


    eeeeMonday, Tuesday, ..., Sunday2


    eeeeeM, T, W, T, F, S, S


    eeeeeeMo, Tu, We, Th, Fr, Su, Sa
    Local day of week (stand-alone)90c2, 3, 4, ..., 1


    co2nd, 3rd, ..., 1st5


    cc02, 03, ..., 01


    cccMon, Tue, Wed, ..., Su


    ccccMonday, Tuesday, ..., Sunday2


    cccccM, T, W, T, F, S, S


    ccccccMo, Tu, We, Th, Fr, Su, Sa
    AM, PM80a..aaaAM, PM


    aaaaa.m., p.m.2


    aaaaaa, p
    AM, PM, noon, midnight80b..bbbAM, PM, noon, midnight


    bbbba.m., p.m., noon, midnight2


    bbbbba, p, n, mi
    Flexible day period80B..BBBat night, in the morning, ...


    BBBBat night, in the morning, ...2


    BBBBBat night, in the morning, ...
    Hour [1-12]70h1, 2, ..., 11, 12


    ho1st, 2nd, ..., 11th, 12th5


    hh01, 02, ..., 11, 12
    Hour [0-23]70H0, 1, 2, ..., 23


    Ho0th, 1st, 2nd, ..., 23rd5


    HH00, 01, 02, ..., 23
    Hour [0-11]70K1, 2, ..., 11, 0


    Ko1st, 2nd, ..., 11th, 0th5


    KK1, 2, ..., 11, 0
    Hour [1-24]70k24, 1, 2, ..., 23


    ko24th, 1st, 2nd, ..., 23rd5


    kk24, 01, 02, ..., 23
    Minute60m0, 1, ..., 59


    mo0th, 1st, ..., 59th5


    mm00, 01, ..., 59
    Second50s0, 1, ..., 59


    so0th, 1st, ..., 59th5


    ss00, 01, ..., 59
    Fraction of second40S0, 1, ..., 9


    SS00, 01, ..., 99


    SSS000, 0001, ..., 999


    SSSS...2
    Timezone (ISO-8601 w/ Z)20X-08, +0530, Z


    XX-0800, +0530, Z


    XXX-08:00, +05:30, Z


    XXXX-0800, +0530, Z, +1234562


    XXXXX-08:00, +05:30, Z, +12:34:56
    Timezone (ISO-8601 w/o Z)20x-08, +0530, +00


    xx-0800, +0530, +0000


    xxx-08:00, +05:30, +00:002


    xxxx-0800, +0530, +0000, +123456


    xxxxx-08:00, +05:30, +00:00, +12:34:56
    Seconds timestamp10t512969520


    tt...2
    Milliseconds timestamp10T512969520900


    TT...2

    Notes:

    1. "Formatting" units (e.g. formatting quarter) in the default en-US locale are the same as "stand-alone" units, but are different in some languages. "Formatting" units are declined according to the rules of the language in the context of a date. "Stand-alone" units are always nominative singular.In format function, they will produce different result:

      format(new Date(2017, 10, 6), 'do LLLL', {locale: cs}) //=> '6. listopad'

      format(new Date(2017, 10, 6), 'do MMMM', {locale: cs}) //=> '6. listopadu'

      parse will try to match both formatting and stand-alone units interchangably.

    2. Any sequence of the identical letters is a pattern, unless it is escaped by the single quote characters (see below). If the sequence is longer than listed in table:

    • for numerical units (yyyyyyyyparse will try to match a number as wide as the sequence

    • for text units (MMMMMMMMparse will try to match the widest variation of the unit. These variations are marked with "2" in the last column of the table.

  • QQQQQ and qqqqq could be not strictly numerical in some locales. These tokens represent the shortest form of the quarter.

  • The main difference between y and u patterns are B.C. years:

    Yearyu
    AC 111
    BC 110
    BC 22-1

    Also yy will try to guess the century of two digit year by proximity with baseDate:

    parse('50', 'yy', new Date(2018, 0, 1)) //=> Sat Jan 01 2050 00:00:00

    parse('75', 'yy', new Date(2018, 0, 1)) //=> Wed Jan 01 1975 00:00:00

    while uu will just assign the year as is:

    parse('50', 'uu', new Date(2018, 0, 1)) //=> Sat Jan 01 0050 00:00:00

    parse('75', 'uu', new Date(2018, 0, 1)) //=> Tue Jan 01 0075 00:00:00

    The same difference is true for local and ISO week-numbering years (Y and R), except local week-numbering years are dependent on options.weekStartsOn and options.firstWeekContainsDate(compare setISOWeekYear and setWeekYear).

  • These patterns are not in the Unicode Technical Standard #35:

    • i: ISO day of week

    • I: ISO week of year

    • R: ISO week-numbering year

    • o: ordinal number modifier

    Values will be assigned to the date in the descending order of its unit's priority. Units of an equal priority overwrite each other in the order of appearance.

    If no values of higher priority are parsed (e.g. when parsing string 'January 1st' without a year), the values will be taken from 3rd argument baseDate which works as a context of parsing.

    baseDate must be passed for correct work of the function. If you're not sure which baseDate to supply, create a new instance of Date: parse('02/11/2014', 'MM/dd/yyyy', new Date()) In this case parsing will be done in the context of the current date. If baseDate is Invalid Date or a value not convertible to valid Date, then Invalid Date will be returned.

    The result may vary by locale.

    If formatString matches with dateString but does not provides tokens, baseDate will be returned.

    If parsing failed, Invalid Date will be returned. Invalid Date is a Date, whose time value is NaN.Time value of Date: http://es5.github.io/#x15.9.1.1

    Usage

    var parse = require('date-fns/parse')

    Syntax

    parse(dateString, formatString, baseDate, [options])

    Arguments

    NameTypeDescription
    dateStringString

    the string to parse

    formatStringString

    the string of tokens

    baseDateDate | String | Number

    defines values missing from the parsed dateString

    options
    (optional)
    Options

    the object with options. See Options

    Properties:
    NameTypeDescription
    additionalDigits
    (optional, default=2)
    0 | 1 | 2

    passed to toDate. See toDate

    locale
    (optional, default=defaultLocale)
    Locale

    the locale object. See Locale

    weekStartsOn
    (optional, default=0)
    0 | 1 | 2 | 3 | 4 | 5 | 6

    the index of the first day of the week (0 - Sunday)

    firstWeekContainsDate
    (optional, default=1)
    1 | 2 | 3 | 4 | 5 | 6 | 7

    the day of January, which is always in the first week of the year

    Returns

    TypeDescription
    Date

    the parsed date

    Exceptions

    TypeDescription
    TypeError

    3 arguments required

    RangeError

    options.additionalDigits must be 0, 1 or 2

    RangeError

    options.weekStartsOn must be between 0 and 6

    RangeError

    options.firstWeekContainsDate must be between 1 and 7

    RangeError

    options.locale must contain match property

    Examples

    // Parse 11 February 2014 from middle-endian format:
    var result = parse(
      '02/11/2014',
      'MM/dd/yyyy',
      new Date()
    )
    //=> Tue Feb 11 2014 00:00:00
    // Parse 28th of February in Esperanto locale in the context of 2010 year:
    import eo from 'date-fns/locale/eo'
    var result = parse(
      '28-a de februaro',
      "do 'de' MMMM",
      new Date(2010, 0, 1),
      {locale: eo}
    )
    //=> Sun Feb 28 2010 00:00:00



    来自  https://date-fns.org/v2.0.0-alpha.18/docs/parse

    普通分类: