• 0

JS Date Conversion


Question

3 answers to this question

Recommended Posts

  • 0

Here is what I've written so far...

function dateFormetter(yyyymmdd) {
    const month = yyyymmdd.substring(4, 6);
    const day = yyyymmdd.substring(6, 8);
    const year = yyyymmdd.substring(0, 4);
    const date = new Date(year, month, day);
    const nameOfMonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    return `${nameOfMonths[date.getMonth()]} ${parseInt(day)}, ${year}`;
}

It doesn't quite seem to work though

  • 0

Date handling in JavaScript is quite limited. If you're doing date handling, I'd recommend using a library instead. The one I tend to use is moment.js.

However, there's not much point involving Date objects in your code though as you basically just pull out the data that you put in anyway so you might as well just deal with the numbers directly like this:

function dateFormatter(yyyymmdd) {

    const nameOfMonths = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];

    const year = parseInt(yyyymmdd.substring(0, 4));
    const month = parseInt(yyyymmdd.substring(4, 6));
    const day = parseInt(yyyymmdd.substring(6, 8));

    return `${nameOfMonths[month - 1]} ${day}, ${year}`;
}

dateFormatter("20240102");   // -> 'Jan 2, 2024'


 

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I would not mind paying 120 up to 150 dollars in this economy value of the dollar currency if their video game is quite breathtaking and excellent use of my hard earned work money. I think people need to appreciate all the effort workers and time that goes into excellent and worthy video games. Now if only the publishers and companies behind the video games would share the love with the developers those at the bottom of work making such games happen as in increase their money accounts and give them a more richer life that I wish would have happened . But how companies have bad reputation of being $$$ profit greedy I feel sad if there are any developers that feel unappreciated as in no real compensation for all the hours of work they put into a blockbuster hit . Have a great day everyone . Thank you developers engineers and programmers that make superb video games happen . You have my thanksgiving and I hope you really really appreciate my words . With you all about this
    • I guess they use the free version, which doesn't send money to any Russians.
    • True, you can pin it to the Start Menu, but my preference is to pin it to the Taskbar since I am using W11 on a desktop with a large display and a lot of space on the Taskbar.
    • 200 mph. Try going 200 mph with a vehicle on a street in any city in America. Good luck. Yes its speculation but so is the idea that it has been 100% approved by the government. When a new drug is under development it goes through a number of test phases, each of increasing efficacy and safety. I would imagine it is the same in this case. You can similuate real conditions on test roads with other cars and if successful on test roads you move it to the next test phase of testing it on real streets with human assistance if needed. Testing conditions on test roads can never account for all real life situations on real roads. This is what I imagine is happening now. The final test phase.
  • Recent Achievements

    • Week One Done
      Hartej earned a badge
      Week One Done
    • One Year In
      TsunadeMama earned a badge
      One Year In
    • Week One Done
      shaheen earned a badge
      Week One Done
    • Dedicated
      Cole Multipass earned a badge
      Dedicated
    • Week One Done
      Alexander 001 earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      571
    2. 2
      ATLien_0
      186
    3. 3
      +FloatingFatMan
      183
    4. 4
      Skyfrog
      112
    5. 5
      Xenon
      106
  • Tell a friend

    Love Neowin? Tell a friend!