Palaniichuk Dmytro
Palaniichuk Dmytro

Reputation: 3183

Convert date and hours/minutes/seconds to correct format javascript

I got date in format '20190702' and hours/minutes/seconds in format '125657' What is the easiest way to convert it to 07/02/2019, 12:56:57

const time = "125657";
  const chuncks = str.match(/.{1,2}/g).join(":"); //12:56:57

What about date?

Upvotes: 0

Views: 95

Answers (7)

tim
tim

Reputation: 492

Assuming fixed length input strings of decimal digits, you could do something like this:

const date = "20190702";
const time = "125657";
const formatted_date = date.replace(/(\d{4})(\d{2})(\d{2})/, "$3/$2/$1");
//const formatted_time = time.match(/\d{2}/g).join(":");
const formatted_time = time.replace(/(\d{2})(\d{2})(\d{2})/, "$1:$2:$3");
const formatted_date_time = `${formatted_date}, ${formatted_time}`;
console.log(formatted_date_time);

Upvotes: 1

trincot
trincot

Reputation: 351308

The easiest is maybe this:

const time = "125657".replace(/(..?)(..)(..)/, "$1:$2:$3");
const date = "20190702".replace(/(....)(..)(..)/, "$2/$3/$1");

console.log(date, time);

The question mark in the first pattern could serve if the time string could have 5 digits instead of 6. If you are certain you always get 6 digits, you can leave it out.

Upvotes: 1

Cuong Le Ngoc
Cuong Le Ngoc

Reputation: 11975

You may use moment.js library to format your date. In your case, the code may look like:

var time = '20190702,125657';
var formated_time = moment(time, "YYYYMMDD,HHmmss").format('DD/MM/YYYY,HH:mm:ss');

Upvotes: 0

Christian Vincenzo Traina
Christian Vincenzo Traina

Reputation: 10464

If the year was 2-digit, you could have used the same exact logic and just add a reverse, but since it's a different length, I advise you a manual composition:

const date = '20190702';

const chunks = date.match(/.{1,2}/g);

const formattedDate = [chunks[3], chunks[2], chunks[0]+chunks[1]].join('/');

console.log(formattedDate)

Upvotes: 0

Shridhar Sharma
Shridhar Sharma

Reputation: 2385

You can just use substr function to extract a part of string and then combine the parts to form new string

const time = "125657";
const date = "20190702";
const formattedTime = `${time.substr(0,2)}:${time.substr(2,2)}:${time.substr(4,2)}`
const formattedDate = `${date.substr(4,2)}/${date.substr(6,2)}/${date.substr(0,4)}`
console.log(`${formattedDate}, ${formattedTime}`)

Upvotes: 3

user996142
user996142

Reputation: 2883

Use Moment.js You provide pattern along with data.

Here, "hh" means "hours" in pattern.

"use strict";
const moment = require("moment");

console.log(moment("125652", "hhmmss").toDate());

Upvotes: 1

Terry Lennox
Terry Lennox

Reputation: 30725

You can use Moment.js to parse dates, it will accept most formats.

let momentDate = new moment.utc('20190702', 'YYYYMMDD');
console.log("Parsed date: ", momentDate);

let momentDateTime = new moment.utc('20190702125657', 'YYYYMMDDHHmmss');
console.log("Parsed date (with time): ", momentDateTime );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Upvotes: 1

Related Questions