Ankush Verma
Ankush Verma

Reputation: 43

Convert date format 27.05.2015 01:46:32.UTC to Locale time

Can anyone help me to convert date format 27.05.2015 01:46:32.UTC to Locale time.

I tried the following code

var date = new Date('27.05.2015 01:46:32.UTC ');
date.toString();

This is also not working. Even momentjs is also gives error to convert this format of date.

Upvotes: 2

Views: 115

Answers (2)

PPB
PPB

Reputation: 3087

The format you are using for Date is DD.MM.YYYY. So you have to change this to MM.DD.YYYY for a valid java script object.

var date = '27.05.2015 01:46:32.UTC';
var date1 = date.split(' ');
console.log(date1)
var date2 = date1[0].split('.');
console.log(date2)
var date3 = date2[1]+ '.' +date2[0] +'.'+ date2[2];
console.log(date3)
var final_date = date3 + ' ' + date1[1];
console.log(final_date);
final_date.toString();

Upvotes: 1

Satpal
Satpal

Reputation: 133403

As per statement even momentjs is also gives error to convert this format of date, I have taken liberty and used here.

You need to use string-format moment constructor to pass string and format in which input string is.

Use

var t = "27.05.2015 01:46:32.UTC"; 

//pass dateTime string and its format, it will return
//moment object
var cdt = moment.utc(t, 'DD.MM.YYYY HH:mm:ss');

//Get date object
var date = cdt.toDate();

//Use date variable as per requiremnt
alert(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment.js"></script>

Upvotes: 1

Related Questions