What is the value of the ISO 8601 duration `P1M` (in seconds)?

前端 未结 2 1569
庸人自扰
庸人自扰 2021-01-18 14:59

Suppose I have an ISO 8601 duration, expressed as \"P1M\". Phrased colloquially, this means \"one month.\" Is there a standard rule for converting this into a n

2条回答
  •  北海茫月
    2021-01-18 15:10

    You are right, an ISO 8601 duration is dependent of the context. A duration is a period/an interval of time between two dates.

    Example :

    2020-01-01/2020-02-01 = P1M = P31D
    2020-02-01/2020-03-01 = P1M = P29D
    2019-02-01/2019-03-01 = P1M = P28D
    

    If you want a fixed duration indepedent of the context, use the day notation P30D, P60D, P90D... instead.

    The same applies for years :

    2019-01-01/2020-01-01 = P1Y = P12M = P365D
    2020-01-01/2021-01-01 = P1Y = P12M = P366D
    

    If you can't have context information about a duration, for example P1M retrieved from database or given by user input, use by default today's context.

    //What is a duration of one month in seconds ?
    P1M = ? (no context)
    //Use default context
    Today = 2020-03-31
    2020-03-31/P1M = 2020-03-31/2020-04-31
    => P1M = P30D
    //A month contains 2 592 000 seconds
    

提交回复
热议问题