Question

I was wondering if there is a way to have conditional formatting based on duration of time; for example lets say I want to look at a timestamp. and if it's been longer than 24 hours from said timestamp the cell will turn the color of red, but if it's under that 24 hour period it will turn the cell the color of green.

Can this be done? .. Thoughts?

Thanks in advance / Kind Regards,

-Sean D.

Was it helpful?

Solution

In the menu Format > Conditional formating... >
you can select the option date is, date is before, date is after apply a condition today, tomorrow, exact date... and then apply a color to these conditions
So in your case you need to apply a color that will be by defect green and add a conditional formating that say if the timestamp is over yesterday then apply color red
EDIT

to compare two timestamp, you can also use the above trick, with a little modifications:
you need to tag in red the cell if it have "!". In the cell, write this formula: =if(E3-E4>1;E3;TEXT(E3;"mm/dd/yyyy hh:mm:ss")&" !") with E3 and E4 the cell where you wrote the two timestamp. you can hide the cell E3 to avoid having the result in duplicate.
otherwise, you'll need to set an app script in your spreadsheet for example:

function compareDate(){
  var ts1 = SpreadsheetApp.getActiveSheet().getRange("E3").getValue();
  var ts2 = SpreadsheetApp.getActiveSheet().getRange("E4").getValue();
  var time1=new Date(ts1).getTime();
  var time2=new Date(ts2).getTime();
  if((time1+86400000)<time2){
    SpreadsheetApp.getActiveSheet().getRange("E3").setBackground("red");
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top