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

120
Views
Format string to date Vuejs

I'm fetching from an API a date string that is formatted like this : DD_MM_YYYY_hh_mm__ss

I want to display the string in this format : DD/MM/YYYY hh:mm:ss

How can I achieve that ?

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

0

You can replace the underscore sequences with the appropriate characters based on their position within the input string:

function convertDate(date) {
    return date.replace(/_+/g, (_, n) => (n < 10) ? "/" : (n > 10) ? ":" : " ");
}

console.log(convertDate("DD_MM_YYYY_hh_mm__ss"));

Or slightly simpler:

function convertDate(date) {
    let n = 0; return date.replace(/_+/g, () => "// ::"[n++]);
}

console.log(convertDate("DD_MM_YYYY_hh_mm__ss"));

about 4 years ago · Juan Pablo Isaza Report

0

One way:

const str = '18_03_2022_12_21_34'
// to string
const res = str.split('_')
const r = res.slice(0, 3).join('/') + ' ' + res.slice(-3).join(':')
console.log(r)
// or to date
console.log(new Date(res.slice(0, 3).join('/').split('/').reverse().join('/') + ' ' + res.slice(-3).join(':')))

about 4 years ago · Juan Pablo Isaza Report

0

Vue Filter

Code:

Vue.filter('stringTodateTime', function ( dateTimeString ) {
    if (!dateTimeString) return ''
    return dateTimeString.replace(/_+/g, (_, n) => (n < 10) ? "/" : (n > 10) ? ":" : " ");
})

Usage:

{{ 12_04_2021_12_59_34 | stringTodateTime }}
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!