1

I am consuming an API that returns a date-time field in form of a string YYYYMMDDHHMMSS.
For the sake of readability for the users who need to see it in a readable form, how can I convert this into dd/mm/yyyy and display the time separately in the form hh:mm:ss?

For example: '20201127100602' convert to: Date: 27/11/2020 , Time: 10:06:02


Any help will be appreciated.
Tim Mwaura
  • 577
  • 1
  • 7
  • 24
  • There are [many duplicates](https://stackoverflow.com/search?q=%5Bjavascript%5D+reformat+date), please make an attempt at an answer before asking for help. – RobG Nov 27 '20 at 21:48

2 Answers2

1

if you're date format is fixed 'YYYYMMDDHHMMSS' then, you could right your own function to get the date time, here is sample code:

var str =  '20201127100602';
var dd = str.match(/.{1,2}/g);

var date = dd[3]+'/'+dd[2]+'/'+dd[0]+dd[1];
console.log(date);

var time = dd[4]+':'+dd[5]+':'+dd[6];
console.log(time)

Here is one more way to readable code suggested by @RobG

    var str =  '20201127100602';
    let [c, y, m, d, hr, min, sec] = str.match(/\d\d/g);
    console.log(d+'/'+m+'/'+c+y);
    console.log(hr+':'+min+':'+sec)

**Note - Recommended only when date format is fixed

Mahesh
  • 137
  • 1
  • 5
0

You could try using the Luxon module (an evolution of Moment.js), it will do this kind of thing very nicely:

const { DateTime } = luxon;

const timestamp = "20201127075434";
const dt = DateTime.fromFormat(timestamp, "yyyyMMddHHmmss");

console.log("dd/mm/yyyy:", dt.toFormat("dd/MM/yyyy"));
console.log("hh:mm:ss:", dt.toFormat("HH:mm:ss"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.25.0/luxon.min.js" integrity="sha512-OyrI249ZRX2hY/1CAD+edQR90flhuXqYqjNYFJAiflsKsMxpUYg5kbDDAVA8Vp0HMlPG/aAl1tFASi1h4eRoQw==" crossorigin="anonymous"></script>

You could also use Moment.js:

const timestamp = "20201127075434";
const dt = moment(timestamp, "YYYYMMDDHHmmSS");

console.log("Moment.js:");
console.log("dd/mm/yyyy:", dt.format("DD/MM/YYYY"));
console.log("hh:mm:ss", dt.format("HH:mm:SS"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • 1
    I'm using nodeJS, is luxon a library available in npm? – Tim Mwaura Nov 27 '20 at 08:01
  • 1
    Thanks very much. I had moment.js but didn't know I could do that. :D – Tim Mwaura Nov 27 '20 at 08:03
  • 1
    Oh yes it is indeed. I've added an example in Moment.js as well. You could use either, I'm liking Luxon now since Moment.js is _kinda_ legacy - "We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done." But Moment.js will be fine to use for a long time yet! – Terry Lennox Nov 27 '20 at 08:04
  • There's no need to parse the string to a Date or use a date library, especially when the OP hasn't tagged or asked for a library. – RobG Nov 27 '20 at 21:42
  • I'd take that point RobG. In this instance, one could simply parse the string, then compose the desired formats. However, using a library gives us a flexible solution that can give us a variety of output formats. One could also convert timezones, for example if the Date is in UTC and we wish to display in another zone. – Terry Lennox Nov 27 '20 at 21:50