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

160
Views
jQuery split date format of string

I have the following date format within a string:

"202112110836"

I want the string to be formatted with a dash - separating the date and the hour separately in the following format:

"2021-12-11 08:36"

How can I achieve this in jQuery?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I don't think such a function exists in jQuery or JavaScript itself for that specific date format; the other solution is to simply fetch each subsection of the string and split it accordingly to the format you want.

const input = "202112110836";

// Fetch each specific date value by substring.
const year = input.substring(0, 4);
const month = input.substring(4, 6);
const day = input.substring(6, 8);
const hour = input.substring(8, 10);
const minute = input.substring(10, 12);

// Combine it all together.
const output = year + "-" + month + "-" + day + " " + hour + ":" + minute;
console.log(output);

about 4 years ago · Juan Pablo Isaza Report

0

We can try a regex replacement for one option:

var ts = "202112110836";
var output = ts.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/, "$1-$2-$3 $4:$5");
console.log(ts + " => " + output);

Note that a more robust approach here would be to parse the text into a bona fide date, then render a text output using the format mask you don't want. I don't know if JS have a date/time API which can do this.

about 4 years ago · Juan Pablo Isaza 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!