Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
JS Format 201708 into M yy format

I have a string in this format 201708 where the first four numbers are the year and the last two the month's. The result being a date of August 2017.

My first idea was to just make it to an date but won't work for me

var formattedDate = new Date("201708")

Thanks for any help

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Separate the date with a hyphen.

let b = "201708"
let b_with_hyphen = b.substring(0, 4) + "-" + b.slice(4)
// '2017-08'

let formattedDate = new Date(b_with_hyphen)
console.log(formattedDate.toUTCString())

about 4 years ago · Santiago Gelvez Report

0

use substring() to split the string into year and month, then use those when calling Date().

let input = '201708';
let year = parseInt(input.substring(0, 4));
let month = parseInt(input.substring(5));
let date = new Date(year, month-1);
console.log(date);

You have to subtract 1 from month because JS counts months starting from 0.

about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!