1

I have the following date format : 2015-02-18T07:07:25.136Z from my database, I would like to convert it to 07:07:25 18-02-2015. I keep on getting the following : 10:07:25 , 18-01-2015. I am using the following function to give me the above date :

var order_date = item.dateordered;
var date = new Date(order_date),
    yr = date.getFullYear(),
    month = +date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth(),
    day = +date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
    hour = +date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),
    minutes = +date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),
    seconds = +date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(),
    cleaned_order_date = hour + ':' + minutes + ':' + seconds + ' , ' + day + '-' + month + '-' + yr;

What is the best way to convert the date from 2015-02-18T07:07:25.136Z to 07:07:25 , 18-02-2015 ?

rrk
  • 15,677
  • 4
  • 29
  • 45
H Dindi
  • 1,484
  • 6
  • 39
  • 68

2 Answers2

2

you could simple use regex replace and capture groups:-

var regex = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).\d{3}[Z]/;
var currentVal = '2015-02-18T07:07:25.136Z';

var newVal = currentVal.replace(regex, '$4:$5:$6 $3-$2-$1') //"07:07:25 18-02-2015"

alert(newVal);
BenG
  • 14,826
  • 5
  • 45
  • 60
0

This may help

var d = new Date();
var date = d.getDate();
var month = d.getMonth() + 1;
var year = d.getFullYear();
var hours = d.getHours();
var mins = d.getMinutes();
var datestring = month + "/" + date + "/" + year + " at " + hours + ":" + mins;

Or

Refer link

HirenPatel
  • 204
  • 2
  • 8