Javascript Data Type How to - Convert UTC date as in '2007-04-06T00:00Z' to mm/dd/yyyy








Question

We would like to know how to convert UTC date as in '2007-04-06T00:00Z' to mm/dd/yyyy.

Answer


<!DOCTYPE html>
<html>
<head>
</head><!--from w  w  w .  j  a  v a2  s .  c om-->
<body>
  <script>
    var dateString = '2007-04-06T00:00Z',
        dateRegExp = /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/,
        match = dateString.match(dateRegExp),
        date;
    if (match) {
      date = new Date(match[1], match[2] - 1, match[3], match[4], match[5]);
      console.log(date);
    }
</script>
</body>
</html>

The code above is rendered as follows: