简体   繁体   中英

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

I have the following JSON object of dates:

[
  {
    "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 .

This is the expected result for 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. (I also checked 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.

How it works:

frStringToDate() function converts your strings to Date objects. 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.

calculateDuration() function subtracts two dates and "converts" the result (in milliseconds) to the Date object. As we know, the new Date(0) is Jan 1, 1970 , so we can subtract 1970 from the year of the result Date object. 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.

getDurationFromObj() is just for code readability

 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)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM