简体   繁体   English

如何使用人性化日期在 JS 中获得本地化的持续时间?

[英]How using humanized date can I get localized duration in JS?

I have the following JSON object of dates:我有以下日期的 JSON 对象:

[
  {
    "date_start": "septembre 2019",
    "date_end": "septembre 2020"
  },
  {
    "date_start": "juin 2017",
    "date_end": "avril 2019"
  },
  {
    "date_start": "novembre 2015",
    "date_end": "Aujourd'hui"
  },
  {
    "date_start": "novembre 2012",
    "date_end": "février 2015"
  },
  {
    "date_start": "novembre 2011",
    "date_end": "octobre 2012"
  }
]

I want to convert those humanized date to a localized duration in en_US and fr_FR .我想将这些人性化的日期转换为en_USfr_FR的本地化持续时间。

This is the expected result for en_US这是en_US的预期结果

Duration syntax does not have to strictly be the one from this example!持续时间语法不必严格使用本示例中的语法!

[
  {
    "date_start": "september 2019",
    "date_end": "september 2020",
    "duration": "1 yrs 1 mo"
  }
]

I am looking at date-fns and so far I have not found how to do it.我正在查看date-fns ,到目前为止我还没有找到如何去做。 (I also checked momentjs ). (我还检查了momentjs )。

Does anybody know how I can convert those in humanized duration?有谁知道我如何在人性化的持续时间内转换那些? I presume this should be converted to date first.我认为这应该首先转换为日期。

You can use this simple pure JS code.您可以使用这个简单的纯 JS 代码。

How it works:这个怎么运作:

frStringToDate() function converts your strings to Date objects. frStringToDate()函数将您的字符串转换为 Date 对象。 It checks string format first and then gets french month name and year from it.它首先检查字符串格式,然后从中获取法语月份名称和年份。 Then it translates (using months object) french month names to english 3-character month abbreviations to finally create a readable string for Date constructor.然后它将(使用months对象)法语月份名称转换为英文 3 个字符的月份缩写,最终为Date构造函数创建一个可读的字符串。

calculateDuration() function subtracts two dates and "converts" the result (in milliseconds) to the Date object. calculateDuration()函数减去两个日期并将结果(以毫秒为单位)“转换”到Date对象。 As we know, the new Date(0) is Jan 1, 1970 , so we can subtract 1970 from the year of the result Date object.我们知道, new Date(0)Jan 1, 1970 ,所以我们可以从结果Date对象的年份中减去 1970 。 To get the months we need to add 1 to the result of .getMonth() function as the months are numbered from 0. Then we can return the result string.要获得月份,我们需要将.getMonth()函数的结果加1 ,因为月份是从 0 开始编号的。然后我们可以返回结果字符串。

getDurationFromObj() is just for code readability getDurationFromObj()只是为了代码可读性

 const months = { 'janvier': 'jan', 'février': 'feb', 'mars': 'mar', 'avril': 'apr', 'mai': 'may', 'juin': 'jun', 'juillet': 'jul', 'août': 'aug', 'septembre': 'sep', 'octobre': 'oct', 'novembre': 'nov', 'décembre': 'dec' } const dates = [{ "date_start": "septembre 2019", "date_end": "septembre 2020" }, { "date_start": "juin 2017", "date_end": "avril 2019" } ] function frStringToDate(frDateString) { const matched = frDateString.match(/^(\\w+) (\\d{4})$/) if (matched) { const frMonth = matched[1] const year = matched[2] const enMonth = months[frMonth] if (!enMonth) return null return new Date(`${enMonth} ${year}`) } return null } function calculateDuration(date1, date2) { const durationMillis = date1 < date2 ? date2 - date1 : date1 - date2 const durationDate = new Date(durationMillis) const years = durationDate.getFullYear() - 1970 const months = durationDate.getMonth() + 1 return `${years} yrs ${months} mo` } function getDurationFromObj(obj) { const date1 = frStringToDate(obj.date_start) const date2 = frStringToDate(obj.date_end) return calculateDuration(date1, date2) } dates.forEach(obj => console.log(getDurationFromObj(obj)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过使用JavaScript输入开放日期和持续时间来获取未来的日期月份和年份 - How can I get future date month and year by inputting opening date and duration month using JavaScript 如何使用 vimeo javascript API 获取视频时长? - How can I get video duration by using vimeo javascript API? 如何从 Moment.js 持续时间中获取特定日期? - How do I get the specific date from a Moment.js duration? 使用原生 JS 获取本地化月份名称 - Get localized month name using native JS 如何在不加载 js 的情况下获得音频的持续时间 - how can i get audio's duration without getting loaded in js 使用moment.js,如何将持续时间简化为最简化的形式? - Using moment.js, How can I simplify a duration to its most simplified form? 如何使用滑块更改anime.js 事件的速度(持续时间)? - How can I change the speed (duration) of an anime.js event using a slider? 如何在三个 js 中获取 animation 的持续时间? - How do I get the duration of an animation in three js? 如何使用 Node.js 获取 Date.now() 的年、日、分、时和秒 - How can I get the year, day, minute, hour, and second of Date.now() using Node.js 我怎样才能在 moment.js/javascript 中人性化这个完整的持续时间 - How can I humanize this complete duration in moment.js / javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM