Javascript: convert datetime to DD/MM/YYYY - Time?

10,708

Solution 1

You can use simple string and array manipulation.

const dateTime = '2017-04-17 18:26:03';
const parts = dateTime.split(/[- :]/);
const wanted = `${parts[2]}/${parts[1]}/${parts[0]} ${parts[3]}:${parts[4]}`;
console.log(wanted);

Additional: If you don't have an environment that supports Template Literals then you can write it like this.

const dateTime = '2017-04-17 18:26:03';
const parts = dateTime.split(/[- :]/);
const wanted = parts[2] + '/' + parts[1] + '/' + parts[0] + ' ' + parts[3] + ':' + parts[4];
console.log(wanted);

Solution 2

You could use a regular expression within a replace call:

input.replace(/^(\d+)-(\d+)-(\d+)(.*):\d+$/, '$3/$2/$1$4');

var input = '2017-04-17 18:26:03';
var result = input.replace(/^(\d+)-(\d+)-(\d+)(.*):\d+$/, '$3/$2/$1$4');
console.log(result);

Explanation

  • ^: match start of the string.
  • (\d+): capture group that matches digits. A captured group can be back-referenced with $1 for the first group, $2 for the second ... etc. in the second argument.
  • :\d+$: match a colon followed by digits and the end of the string ($): as this is not captured, this part (seconds) will be omitted in the result.

Solution 3

try to create a function that format your date. here is an example that i wrote.

function formate(date) {
    if (typeof date == "string")
        date = new Date(date);
    var day = (date.getDate() <= 9 ? "0" + date.getDate() : date.getDate());
    var month = (date.getMonth() + 1 <= 9 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1));
    var dateString = day + "/" + month + "/" + date.getFullYear() + " " + date.getHours() + ":" + date.getMinutes();

    return dateString;
}
console.log(formate("2017-04-17 18:26:03"));

Solution 4

This will do the work:

var timestamp = Date.parse('2017-04-17 18:26:03'); // 1492467963000
var date = new Date(timestamp).toJSON();           // "2017-04-17T22:26:03.000Z"
var dateStr = date.slice(0, 10).split("-").reverse().join("/") // "17/04/2017"
                .concat(' ')
                .concat(date.slice(11, 16));       // "22:26"

console.log(dateStr)
"17/04/2017 22:26"
Share:
10,708
David Hope
Author by

David Hope

Updated on July 27, 2022

Comments

  • David Hope
    David Hope almost 2 years

    I have a datetime that looks like this:

    2017-04-17 18:26:03
    

    How can I convert this to this format using javascript or jquery:

    17/04/2017 18:26
    

    I found this question which I thought might help me but the answers are converting a timestamp but mine is not a time stamp.

    How to convert a DateTime value to dd/mm/yyyy in jQuery?

  • streetturtle
    streetturtle about 7 years
    What's wrong? He knows how to convert timestamp to date, I showed how to convert string to timestamp... If I misunderstood the question it's not the reason to downvote. Just let me know what I did wrong in comments to my answer - it's simple.
  • David Hope
    David Hope about 7 years
    Unfortunately there are quite a few trigger happy fingers on STO. you could be giving a correct answer and still get downvoted. same goes for the questions.. you could be asking a valid question and still get downvoted.
  • Ranadip Dutta
    Ranadip Dutta about 7 years
    Thats a hell lot of regex. :) Could you please explain me. I am trying to understand it
  • trincot
    trincot about 7 years
    Added some explanations... Tell me if something needs clarification.
  • Xotic750
    Xotic750 about 7 years
    I don't the ^ in your answer?
  • trincot
    trincot about 7 years
    @Xotic750, Added it just now. I only had it in the snippet. Works either way, but I prefer to include it as it makes clear we want to have a match on the complete input, not just the ending part of it.
  • Rudy Vissers
    Rudy Vissers about 7 years
    I didn't downvote your answer. You should not make reference to a 'link'. You should explain what is in the 'link'. The answer is a little confusing.
  • David Hope
    David Hope about 7 years
    remove the ``` from around the ${parts[2]}/${parts[1]}/${parts[0]} ${parts[3]}:${parts[4]}. it took me hours to figure out why this code was throwing an error. other than that it works fine.
  • Xotic750
    Xotic750 about 7 years
    They are required with how it is currently written. It is a Template Literal If you remove them then you must use string concatenation.
  • Xotic750
    Xotic750 about 7 years
    I don't know how, without them you should get SyntaxError: Unexpected token { Can you provide a jsfiddle that demonstrates it working without them?