Question

I have a search script that I am using to search a spreadsheet. One of the returned values is a date. On the spreadsheet it looks like mm/dd/yyyy but in the text box it shows a full date like Fri May 10 2013 03:00:00 GMT The code line looks like:

txt1.setText(data[nn][14]);

How do I make it look like mm/dd/yyyy 01/26/2013? I tried

var date = (Date(data[nn][14]), "EST", "MM/dd/yyyy") but it literally shows MM/dd/yyyy

Also I have txt1.setText(data[nn][0]).setStyleAttribute("background", "cyan"); That colors important boxes. Just as a visual aid. On the spread sheet that date has conditional formatting that turns it red of the date is before the current date. (Expired) Is there a way to make the script look for that formatting or preform the same conditional format?

This is what I could come up with,

if(data[nn][14] < Date(), .setStyleAttribute("background", "red"));

But it doesn't work

Was it helpful?

Solution

Try converting your date objects to "milliseconds from the epoch" using getTime().

var dateSerial = data[nn][14].getTime();
var nowSerial = (new Date()).getTime();
var formatColour = (dateSerial < nowSerial)?'red':'cyan';

For the other part of your question (specifying how you want the date to appear), take a look at Utilities.formatDate().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top