admin管理员组

文章数量:1200335

I am having some difficulties when trying to convert UTC Date format to dd/mm/yyyy in JavaScript:

var launchDate = attributes["launch_date"];
if (isBuffering) {
         var date = new Date(launchDate);
         var d = new Date(date.toLocaleDateString());
         launchDate = ((d.getUTCMonth() + 1) + "/" + (d.getUTCDate() + 1) + "/" + (d.getUTCFullYear()));
 } 

I tried with this, but it returns me an invalid date. So I changed to this:

var launchDate = attributes["launch_date"];
if (isBuffering) {
         var date = new Date(launchDate);
         var d = formatDate(new Date(date.toLocaleDateString()));
         launchDate = ((d.getUTCMonth() + 1) + "/" + (d.getUTCDate() + 1) + "/" + (d.getUTCFullYear()));
 }

However, it still returning me invalid Date. I wonder is there any possible way to change the date format of Fri May 31 2013 17:41:01 GMT+0200 (CEST) to dd/mm/yyyy?

Thanks in advance.

I am having some difficulties when trying to convert UTC Date format to dd/mm/yyyy in JavaScript:

var launchDate = attributes["launch_date"];
if (isBuffering) {
         var date = new Date(launchDate);
         var d = new Date(date.toLocaleDateString());
         launchDate = ((d.getUTCMonth() + 1) + "/" + (d.getUTCDate() + 1) + "/" + (d.getUTCFullYear()));
 } 

I tried with this, but it returns me an invalid date. So I changed to this:

var launchDate = attributes["launch_date"];
if (isBuffering) {
         var date = new Date(launchDate);
         var d = formatDate(new Date(date.toLocaleDateString()));
         launchDate = ((d.getUTCMonth() + 1) + "/" + (d.getUTCDate() + 1) + "/" + (d.getUTCFullYear()));
 }

However, it still returning me invalid Date. I wonder is there any possible way to change the date format of Fri May 31 2013 17:41:01 GMT+0200 (CEST) to dd/mm/yyyy?

Thanks in advance.

Share Improve this question asked Mar 27, 2014 at 12:53 user2531590user2531590 1
  • There are only so many date formats Javascript natively recognizes. If you need a specific format that's not among those, you may have to engage in some custom object(s) that do the "in house" conversion to/from the formats you need to one that Javascript recognizes natively. Have gone down this road before, can be surprisingly tricky and land-mine prone :) – David W Commented Mar 27, 2014 at 12:57
Add a comment  | 

4 Answers 4

Reset to default 12
var d = new Date();
var n = d.toLocaleDateString();

This will be more superior in build JS method!

function formatDate(d)
 {
  date = new Date(d)
  var dd = date.getDate(); 
  var mm = date.getMonth()+1;
  var yyyy = date.getFullYear(); 
  if(dd<10){dd='0'+dd} 
  if(mm<10){mm='0'+mm};
  return d = dd+'/'+mm+'/'+yyyy
}

Try it:

Date.parseExact(Your_Date, 'dd/MM/yyyy').toString('MM/dd/yyyy');

or

Date.parseExact(Your_Date, 'MM/dd/yyyy').toString('dd/MM/yyyy');

Month is 0 indexed, but day is not. You don't need to add 1 to your day.

Also, you're formatting it for MM/dd/yyyy, not dd/MM/yyyy.

solution:

    var launchDate = attributes["launch_date"];
    if (isBuffering) {
             var date = new Date(launchDate);
             var d = formatDate(new Date(date.toLocaleDateString()));
             launchDate = ((d.getUTCDate())+ "/" + (d.getUTCMonth() + 1) +  "/" + (d.getUTCFullYear()));
     }

本文标签: javascriptConvert UTC Date to ddmmyyyy FormatStack Overflow