0

I am using two functions to extract both the date & time out of a string:

Raw String in DATA!A2: 2022-03-06T04:52:33.813Z

=LEFT(DATA!$A$2,FIND("T",DATA!$A$2)-1) to reformat a string into a date:

Result: 2022-03-06

In another cell I am extracting the time and converting it to USA CT time: =MID(DATA!$A$2,12,5)-"6:00"

Result: 10:52 PM


The Issue:

I have successfully subtracted 6 hours to convert this time to CT, but now whenever the time is on or after 6 PM CT, the date (2022-03-06) prematurely advances to the next day. In the example above, because 10:52 PM is after 6 PM, the date should be showing 2022-03-05.

Are there any ways to check if the time is after 6 PM, and if that is TRUE, to correct the date by -1 days?

halfer
  • 19,824
  • 17
  • 99
  • 186
WebMW
  • 514
  • 2
  • 13
  • 26

2 Answers2

1

Since Google Sheets does not recognize the raw string as a date, the best option for this would actually be to create a custom function which converts the string into a date. In this way, it will be easier to perform the date operations you want.


To do so, you will have to go to Extensions > Apps Script and use the following code for the custom function from here:

/**
 * Converts a string to a date-time value
 * @customfunction
 */
function DATETIME(dateTimeString) {
  var output = new Date (dateTimeString);
  if(!isNaN(output.valueOf())) {
    return output;
  } else {
    throw new Error('Not a valid date-time');
  }   
}

Save the project, run it, authorize the script and return to the sheet.

Afterwards, in your sheet, call the DATETIME function as you would with an already existing Sheets function and subtract the hours using the TIME function:

=(DATETIME(A2)-TIME(7,0,0))

Assuming your sheet looks like this, the resulting column will look like this:

result sheet

If you also want to separate the date from the time, you will have to add two more formulae (assuming that D1 contains the result from the DATETIME custom function):

  • For retrieving the date:
=LEFT(D1,FIND(" ",D1)-1) 
  • For retrieving the time:
=RIGHT(D1,LEN(D1)-FIND(" ",D1))

After all the formulae, this is how the sheet will look like:

end result after applying all the formulae

Reference

ale13
  • 5,679
  • 3
  • 10
  • 25
1

A-

Try, without any script

=value(left(A1,10))+value(mid(A1,12,12))

enter image description here

B-

If you want to subtract 6 hours, try

=value(left(A1,10))+value(mid(A1,12,12))-6/24

you will change the date if any

C-

However, to solve that issue I have successfully subtracted 6 hours to convert this time to CT, but now whenever the time is on or after 6 PM CT, the date (2022-03-06) prematurely advances to the next day.

1- check the timezone of your spreadsheet

2- AND the timezone of your script editor.

To do that in script editor, check the parameters

  • click on the gear in the left hand side) and check the third parameter
  • go back to the scripts
  • modifie time zone in appscript.json and make sure it is the same as your spreadsheet (for instance Europe/paris)

enter image description here

Mike Steelson
  • 14,650
  • 2
  • 5
  • 20
  • Thank you! Both answers would have been a solution but I prefer not having to use a script if it can be done with the right formula. Genius work on the subtracting of 6 hours in this "Date Time" format... that was very much needed, rather than having the fields separate. I also split the date & time into two separate fields afterwards as @ale13 suggested. Thank you Mike! – WebMW Mar 08 '22 at 00:40