Search code examples
javascriptdatemomentjsjulian-date

How to transform YYYYDDD to DD-MM-YYYY


I have a date which is given in YYYYDDD format, where YYYY is the year and DDD the days through the year (001-365).

Does the library from moment.js or any other library has a conversor for this kind of given dates to a modern date, like DD-MM-YYYY or something similar?


Solution

  • See the moment-documentation: https://momentjscom.readthedocs.io/en/latest/

    // Parse string in format YYYYDD
    let momentdate = moment('2023050', "YYYYDDD");
    
    // Convert to format DD-MM-YYYY
    let datestring = momentdate.format("DD-MM-YYYY");
    
    console.log(datestring);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>