We can convert a timestamp to Unix time (in milliseconds) in Google Sheets. For that, we can use a formula that only involves date functions and arithmetic operators.
Before attempting to convert a timestamp to milliseconds, you must know about Unix Epoch.
Unix, as well as Unix-like operating systems, measure system time from Jan 1st; 1970 00:00:00 UTC. The term Unix Epoch is used for describing this starting point of time.
Unix time is the number of seconds that have elapsed since the above Unix Epoch. In this Google Sheets tutorial, we are going to convert a timestamp or date to Unix time in milliseconds.
That means the number of milliseconds elapsed since Unix Epoch, i.e. from 00:00:00 Thursday, 1 January 1970.
Before writing the formula, you should know how to convert a local time to GMT/UTC in Google Sheets. That is important to write the converter code/formula.
Convert Local Time to GMT/UTC in Google Sheets
Enter the =now()
function in cell A1 to get the local time. If this formula is not returning the correct local time in your Sheet, you need to follow the below steps.
Steps:
- Click the menu FILE and open “Spreadsheet settings”.
- Under the “General” tab see the selected time zone. Correct that to your time zone.
Now you have set the correct local time in Google Sheets. In the next step, you will get the formula to convert a timestamp in local time to GMT/UTC time.
See the above screenshot. If your local time is GMT+5:30:00 (which is correct in my case), use the formula below.
=A1-5.5/24
If your local time is GMT-7:00:00, the formula to convert local time to GMT/UTC will be as follows.
=A1+7/24
Note: If your time zone is GMT+, in the formula use the minus sign. If it is GMT- then use the plus sign.
Google Sheets Formula to Convert Timestamps to Milliseconds
Formula to Convert Timestamp aka Date-Time to Unix Time in Milliseconds:
I have the following date-time in cell A1.
4/8/2017 11:30:00
The above formula will convert this date-time to 1501826400000 milliseconds.
To convert the current date-time to milliseconds in Google Sheets, use the below formula.
=(now()-5.5/24-DATE(1970,1,1))*86400000
In this formula, I have replaced A1 with the now() function which returns the current date and time.
Additional Tips:
- Use
=(now()-5.5/24-DATE(1970,1,1))*864000
formula to convert to seconds. - And this for microseconds –
=(now()-5.5/24-DATE(1970,1,1))*86400000000
.
Related:- Convert Unix Timestamp to Local DateTime and Vice Versa in Google Sheets.