• 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

    • Flip flop flip flop. This guy has no idea what he's doing.
    • Trump announces a 25% tariff on India, leaving smartphone manufacturers stunned by Hamid Ganji On Wednesday, President Trump announced that the US administration had decided to impose a 25 percent tariff on imported goods and products from India. The tariffs will take effect on Friday. In addition, India may face further penalties for engaging in trade with Russia, including the purchase of Russian oil. Trump said India is a friend to the United States, whose "tariffs are far too high, among the highest in the world." He also criticized India's weapons and oil deals with Russia, "when everyone wants Russia to STOP THE KILLING IN UKRAINE." India currently applies a 39 percent tariff on imported agricultural products and a 45 percent tax on vegetable oils. In response to the US's new tariffs, the Indian government said it's examining the implications of Trump's announcement while continuing the negotiations with Washington for a "fair, balanced and mutually beneficial bilateral trade agreement." The 25 percent tariff places India among the countries subject to the highest import duties when exporting to the US market. By comparison, imports from the European Union face a tariff of just 15 percent. After officially taking office, President Trump moved to further escalate the trade war with China by raising tariffs to their highest levels. As the world's leading manufacturer of smartphones for major brands such as Apple, Google, and Samsung, China's increased tariff burden was expected to drive up smartphone prices in the US market. The increase in tariffs on China has prompted an increasing number of tech companies to shift their manufacturing focus to India. For example, most iPhones sold in the US are now Indian-made, while the latest data by Canalys shows around 44 percent of all smartphones imported to the US are also made in India. While smartphone manufacturers initially hoped that shifting production to India would protect them from tariffs, the recent imposition of a 25 percent tariff has complicated the situation significantly. In April, the US government imposed a 27 percent tariff on imports from India, but later backed away from the plan. Now, however, it seems that the two sides have failed to find common ground in their trade negotiations.
    • OpenAI to build giant AI hub in Norway, tightening US grip on Europe's tech future by Paul Hill OpenAI has announced Stargate Norway, its first AI data center initiative in Europe under the OpenAI for Countries program. It has a planned 230MW capacity and is expected to host 100,000 Nvidia GPUs by the end of next year, with a significant future expansion hoped for. The site will be built by Nscale and Aker will help on the energy side of things, they will form a 50/50 joint venture, owning the site. The Stargate Norway announcement follows Stargate UAE and other partnerships, indicating that OpenAI is looking at a global strategy for its infrastructure needs. Stargate Norway will run entirely on renewable hydropower in Narvik, Norway. OpenAI cited low-cost energy, cool climate, and mature industrial base, explaining that these make it an ideal place for the project to take shape. The facility will run on 100% renewable energy and will use closed-loop, direct-to-chip liquid cooling to ensure maximum cooling efficiency. The excess heat from the GPU systems will be made available to help support low-carbon enterprises in the region. While renewable energy is often seen as an ethical choice, in the case of Stargate Norway, it is being chosen for entirely practical reasons. The data center will require loads of energy to power AI so using Norway’s hydropower makes a lot of sense. OpenAI said that the project aims to deliver on Norway’s sovereign AI goals and provide sovereign compute capacity in Europe. The announcement also stated that Aker and Nscale will provide priority access to Norway’s AI ecosystem and any surplus capacity will be available to the public and private sectors across the UK, Nordics, and Northern Europe. With the establishment of Stargate Norway, the country expects to see new jobs created, more economic activity, and AI research collaboration with local institutions. OpenAI called the deal “one of the most ambitious AI infrastructure investments in Europe to date.”
    • Because it's just a one liner fix. One person ( StartAllBack dev ) was able to fix it... in a cave!
    • I have a first draft of the setup script available here if anybody is curious.  It'll work well enough for me, but obviously adapt it to your own needs as necessary. Link: https://gitlab.com/-/snippets/4876568
  • Recent Achievements

    • Week One Done
      whiloh earned a badge
      Week One Done
    • Week One Done
      memnoch earned a badge
      Week One Done
    • First Post
      UAVXP earned a badge
      First Post
    • Dedicated
      Xinotema earned a badge
      Dedicated
    • Rookie
      MrNukes went up a rank
      Rookie
  • Popular Contributors

    1. 1
      +primortal
      661
    2. 2
      ATLien_0
      205
    3. 3
      Xenon
      133
    4. 4
      neufuse
      124
    5. 5
      Michael Scrip
      123
  • Tell a friend

    Love Neowin? Tell a friend!