简体   繁体   中英

Converting any kind of date format to MM\DD\YYYY

I am converting this 2 sets of date to the format MM\\DD\\YYYY

1. Thu Aug 31 15:00:00 GMT+08:00 2017

  1. 2017-08-09

When I'm converting the 1st one I use this code.

var STD_Date = STD_data[i][4]; //<----This is where the date comes.
var date = convertDate(STD_Date);
var datearray = date.split("/");
var New_STDDate = datearray[1] + '/' + datearray[0] + '/' + datearray[2];

This is the function convertDate()

function convertDate(inputFormat) {
  function pad(s) { return (s < 10) ? '0' + s : s; }
  var d = new Date(inputFormat);
  return [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/');
}

This is how I format the second one.

This is the function

  var toMmDdYy = function(input) {
    var ptrn = /(\d{4})\-(\d{2})\-(\d{2})/;
    if(!input || !input.match(ptrn)) {
        return null;
    }
    return input.replace(ptrn, '$2/$3/$1');
};

This is how I use it.

var startdate = form.startdate //<--- comes from HTML Picker (Format "YYYY-MM-DD")
toMmDdYy(startdate)

My question is this how can I have a function that will format the date whether it is the 1st or the 2nd one?

Convert_TimeStamp_Date(){
  //This is where to code will go to convert
  //to MM\DD\YYYY
}

//Then call it
var startdate = "2017-08-08"
var timestamp = "Thu Aug 31 15:00:00 GMT+08:00 2017"
Convert_TimeStamp_Date(startdate);
Convert_TimeStamp_Date(timestamp);
//both of them the output must be "MM\DD\YYYY"

This is the current code but looking forward for a better one. WORKING

//Time Stamp to MM\DD\YYYY
function convertDate(inputFormat) {
  function pad(s) { return (s < 10) ? '0' + s : s; }
  var d = new Date(inputFormat);
  var chopdate = [pad(d.getDate()), pad(d.getMonth()+1), d.getFullYear()].join('/');
  var datearray = chopdate.split("/");
  var newdate  = datearray[1] + '/' + datearray[0] + '/' + datearray[2];
  return newdate;
}

//YYYY-MM-DD tp MM\DD\YYYY
var toMmDdYy = function(input) {
    var ptrn = /(\d{4})\-(\d{2})\-(\d{2})/;
    if(!input || !input.match(ptrn)) {
        return null;
    }
    return input.replace(ptrn, '$2/$3/$1');
};

//Convert Date based on input to MM\DD\YYYY
function ConverSpedDate(input){
   if( input.lenght > 10 ) return toMmDdYy(input);
    return convertDate(input);
}

This should work

convertDate = function( input ){
    if( input.lenght > 10 ) return convertDate( input );
    return toMmDdYy( input );
}

You can test each pattern and reformat accordingly. Your reformatting functions appear to be cumbersome and error prone, consider the following:

 var startdate = "2017-08-23" var timestamp = "Thu Aug 31 15:00:00 GMT+08:00 2017" function reformatTimestamp(s) { if (/\\d{4}-\\d\\d-\\d\\d/.test(s)) { return reformatISOtoMDY(s); } else if (/[az]{3} [az]{3} \\d\\d \\d\\d:\\d\\d:\\d\\d \\w{3}\\+\\d\\d:\\d\\d \\d{4}/i.test(s)) { return reformatCustomToMDY(s); } } // Reformat YYYY-MM-DD to MM\\DD\\YYYY function reformatISOtoMDY(s) { var b = s.split(/\\D/); return b[1] + '\\' + b[2] + '\\' + b[0]; } function reformatCustomToMDY(s) { var months = '. jan feb mar apr may jun jul aug sep oct nov dec'.split(' '); var b = s.split(/ /); return ('0' + months.indexOf(b[1].toLowerCase())).slice(-2) + '\\' + ('0' + b[2]).slice(-2) + '\\' + b[5]; } console.log(reformatTimestamp(startdate)) console.log(reformatTimestamp(timestamp)) 

The format MM\\DD\\YYYY is unusual and likely to confuse.

As you've tagged this as a GAS question, have you looked at Utilities.formatDate()? Documentation here , but in short it takes 3 parameters: a date object, a time-zone string & a format string. The TZ & format are taken from the Java SE SimpleDateFormat class.

In your instance, try this:

var ts = "Thu Aug 31 15:00:00 GMT+08:00 2017";
var d = new Date(ts);
Logger.log(Utilities.formatDate(d, "GMT+08:00", "MM/dd/yyyy")); // logs 08/31/2017

Note that you will have to set the time-zone in the output yourself. You could extract it from the timestamp via a regex, for example. As the JS Date primitive is milliseconds 1970-01-01T00:00:00 UTC, you can set your output TZ to suit your needs.

I also +1 the recommendations to stick to ISO date & time formats: MM/dd/yyyy absent locale information is just asking for trouble.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM