0
var string = "14/2/2018 6:00 pm";

How to convert the String variable to Date format with UTC Time zone? i try this var x = moment.tz("16/2/2018 7:00 pm", "UTC").format(); output: x : Invalid Date

jit
  • 35
  • 1
  • 1
  • 6

2 Answers2

0

You need to use this format while parsing in moment

'DD/M/YYYY h:mm a'

var string = "14/2/2018 6:00 pm";
var date = moment(string, 'DD/M/YYYY h:mm a').toDate();
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js"></script>
Durga
  • 15,263
  • 2
  • 28
  • 52
0

You will find it here: https://momentjs.com/docs/

For instance:

Unless you specify a time zone offset, parsing a string will create a date in the current time zone.

moment("2010-10-20 4:30", "YYYY-MM-DD HH:mm"); // parsed as 4:30 local time moment("2010-10-20 4:30 +0000", "YYYY-MM-DD HH:mm Z"); // parsed as 4:30 UTC

=>

string += ' +0000';
moment(string, 'DD/M/YYYY h:mm a Z');
D. Braun
  • 508
  • 1
  • 4
  • 11