Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The simplest way of check if a data type of an element in an array is a date object?

I want to create a condition that checks if a data type of an element in an array (formed by data from a spreadsheet) is a date object (so, I can manipulate the string format of this date, because I don't want a date like this: Thu May 23 2013 00:00:00 GMT-0400 (EDT) but like this: 23/05/2013).

I get the date from the google spreadsheet using this function:

function getRowAsArray(sheet, row) {
  var dataRange = sheet.getRange(row, 1, 1, 99);
  var data = dataRange.getValues();
  var columns = [];
  for (i in data) {
    var row = data[i];
    Logger.log("Got row", row);
    for(var l=0; l<16; l++) {
        var col = row[l];
        columns.push(col);
    }
  }
  return columns;
}

Supose my data type object can be in one of the data[i] array elements. What is simplest way of doing it?

var text = data[i];

Supose my data type object can be in one of the data[i] array elements. What is simplest way of doing it? It was sugested in one awnswer to do something like this

 var data = getRowAsArray(sheet, sheet);
    for(var i=0; i<columns.length; i++) {
    var key = ":" + columns[i] + ":";

    if (data[i] instanceof Date) {
        var d = data[i]; //date from data[i]
        var m = d.getMonth() + 1; //months starts at 0
        var y = d.getFullYear();
        var day = d.getDate();
        data[i] = day + "/" + m + "/" +y;
    }          

Thanks for any help!

like image 482
craftApprentice Avatar asked Nov 24 '25 02:11

craftApprentice


1 Answers

You can use the instanceof keyword.

if (data[i] instanceof Date) {
    //it's a date
} else {
   //it's not
}

To format the date, you can do it like:

var d = new Date(), //date from data[i]
    m = d.getMonth() + 1, //months starts at 0
    y = d.getFullYear(),
    d = d.getDate();

console.log([d, m, y].join('/'));
like image 144
plalx Avatar answered Nov 26 '25 18:11

plalx