111

I have a sheet with a column of unix epoch times (in seconds): 1500598288

How can I convert these into normal dates?

Rubén
  • 34,714
  • 9
  • 70
  • 166
Mike Bjorge
  • 1,941
  • 2
  • 11
  • 13

8 Answers8

201

As of 2023, GSheet introduced a new function, EPOCHTODATE:

=EPOCHTODATE(1500598288)

To convert a whole column of numbers, just use ARRAYFORMULA (or BYROW, if you are LAMBDA-inclined):

=ARRAYFORMULA(EPOCHTODATE(A:A)

enter image description here  

--- The answer below still works, but is outdated as of 2023 ---

The simplest way, not requiring any JS programming, would be through a formula, dividing by 86400 seconds per day and adding to January 1, 1970. For example the following gives 21 July 2017:

=1500598288 / 86400 + DATE(1970, 1, 1)

To convert a whole column of numbers, just use ARRAYFORMULA:

=ARRAYFORMULA(A:A / 86400 + DATE(1970, 1, 1))
ttarchala
  • 4,277
  • 2
  • 26
  • 36
  • 2
    Also, Unix time is UTC(Coordinated Universal Time). Considering your time zone, for example, I'm in San Francisco, it would be – Jiayang Sep 16 '17 at 06:32
  • please amend typo in number of seconds: https://productforums.google.com/d/msg/docs/cXhne_Ys_QU/un3q9nVR3qMJ – lbrutti Nov 02 '17 at 08:52
  • Hi @lbrutti, what exactly are you referring to? What is the typo? – ttarchala Nov 03 '17 at 09:09
  • 18
    It's 2017 and Google, the land of the power users, don't think anyone wants to convert timestamps to human readable dates in a spreadsheet. I must be living in the wrong universe. – Sridhar Sarnobat Dec 05 '17 at 01:54
  • @Sridhar-Sarnobat. If you have clients that want to peruse your spreadsheet and have no idea what a unix timestamp is. – nathan Dec 18 '17 at 13:22
  • 20
    Nice!! And the reverse is: `=(B71-date(1970,1,1))*86400` – Menelaos Dec 29 '17 at 01:52
  • @MenelaosBakopoulos what would B71 mean in this case? the cell with date data? – Shaardool Jan 21 '18 at 12:07
  • 1
    @Shaardool , yes the cell with the data! – Menelaos Jan 22 '18 at 09:30
  • A simple copy-paste of your formula gives me #ERROR in google sheets. – Ignacio Soler Garcia Sep 24 '20 at 08:54
  • @IgnacioSolerGarcia What is the error's details when you hover over it? This formula is so simple that it's hard to imagine how it can fail... maybe you placed it in the A column itself, which contains the data, then it would be a circular reference. – ttarchala Sep 25 '20 at 09:06
  • @ttarchala it is not a circular reference and it just says the formula is wrong, this one works tho: A2/60/60/24 + DATEVALUE("1970/1/1") – Ignacio Soler Garcia Oct 08 '20 at 10:14
  • 7
    Useful to note here is that, if you want to convert a Unix timestamp to your own timezone, you should also add `time(1, 0, 0)` to go to UTC+1, or subtract `time(1, 0, 0)` to go to UTC-1, etc. – Joeytje50 Mar 11 '21 at 13:14
  • Thanks! Two additions for it to work for me: my timestamp was text, so I needed to convert with `VALUE()`, and the timestamp had milliseconds, so `/ 1000 `, like: `=arrayformula(VALUE(A:A) / 86400 / 1000 + date(1970,1,1))` – ptim Mar 25 '21 at 02:38
  • how do you format this to show as a date? – dcsan Nov 18 '21 at 16:27
  • @dcsan Just use the usual formatting button "123▼" on the Sheets toolbar. – ttarchala May 12 '22 at 07:40
  • @ttarchala For languages that use comma as a decimal separator, the parameters should be separated by semicolons. – LHLaurini Jun 18 '22 at 13:06
  • @LHLaurini Of course, but this is not specific to the question or to the answer - it's true for all formulas and people who write any formula are expected to know this already. – ttarchala Jun 18 '22 at 14:22
  • 1
    Sorry, I meant to tag @IgnacioSolerGarcia, since that's probably why it wasn't working for them – LHLaurini Jun 19 '22 at 00:36
45

Make a custom function using the javascript Date function:

Tools > Script Editor... >

function FROM_UNIX_EPOCH(epoch_in_secs) {
  return new Date(epoch_in_secs * 1000);  // Convert to milliseconds
}

And then in a new column you can do =FROM_UNIX_EPOCH(A1)

Mike Bjorge
  • 1,941
  • 2
  • 11
  • 13
  • 1
    Note that you now need a JSDoc comment with @customfunction in order for your script to be usable in the sheet. Source: https://developers.google.com/apps-script/guides/sheets/functions – Spencer May 08 '22 at 22:39
13

Add this for Epoch Timestamp to DateTime (GMT adjustment) in google sheets cell

=A1/86400+DATE(1970,1,1)+time(5,30,0)

UserBSS1
  • 2,091
  • 1
  • 28
  • 31
12

now, 3 years later from the original post, you can make...

Tools > Script Editor... >

function millisToDate(timeInMillis){
    var yourDateFromMillis = new Date(timeInMillis);
    return yourDateFromMillis;
}

and use your new function in the cell...

=millisToDate(cell here)

loopmode
  • 617
  • 8
  • 14
  • 1
    I use the above function a lot. I'm adding some bonus items here. :) You can set to locale time also. For example: function millisToDate(timeInMillis){ var yourDateFromMillis = new Date(timeInMillis).toLocaleString('en-US', { timeZone: 'Asia/Kolkata' }); return yourDateFromMillis; } Timezones are here. https://en.wikipedia.org/wiki/List_of_tz_database_time_zones – Jason Allshorn Jan 03 '22 at 02:35
  • this answer works in google sheets, while the FROM_UNIX_EPOCH() above does not. – KevinDeus Mar 02 '22 at 05:22
  • This is not working for me. If I use: 1665578823 / Wed Oct 12 2022 12:47:03 GMT+0000 The value in the sheet is 20/01/1970 – decodebytes Oct 21 '22 at 14:30
8

Building on the accepted answer, you will want to use FLOOR() if you want to be able to compare the dates

=FLOOR(1500598288/86400)+date(1970,1,1)

Otherwise, "7/21/2017" in one cell might not equal "7/21/2017" in another cell.

emmby
  • 99,783
  • 65
  • 191
  • 249
5

Building on the other awesome answers, here's what I use to get the full date time from an epoch timestamp. Tested with Google Sheets.

=QUOTIENT(A1, 86400) + date(1970, 1, 1) + time(quotient(MOD(A1, 86400), 3600), quotient(mod(A1, 3600), 60), mod(A1, 60))

Some examples:

epoch date time
0 1970-01-01 0:00:00
61 1970-01-01 0:01:01
3599 1970-01-01 0:59:59
86403 1970-01-02 0:00:03
2678402 (=86400*31+2) 1970-02-01 0:00:02
31536008 (=86400*365+8) 1971-01-01 0:00:08
Liu Zehua
  • 421
  • 5
  • 3
1

In AppScript Paste this :

function EpocToDate(epoch) {
  //var epoch = 1451606400000; 12/31/2015 19:00:00
  return (Utilities.formatDate(new Date (epoch),'America/New_York','MM/dd/yyyy HH:mm:ss'));
}

Use in Google Sheets as:

=EpocToDate(1451606400000)

Incase if you have to refer from another cell :

=EpocToDate(VALUE(C2))
Rubén
  • 34,714
  • 9
  • 70
  • 166
-7

The formula given in the answer by ttarchala is incorrect. Here is a more expansive answer.

I'd recommend not replacing the data in the of seconds since epoch times column. You may need it for sorting, etc. Additionally, it will be difficult to apply the formula to an entire column. Instead, initially insert a new column next to it.

Click in the new cell adjacent the topmost epoch cell. Assuming that the topmost epoch cell is A1, enter this formula into the formula box for the new cell.

=A1/8640000+date(1970,1,1)

The date time should be displayed in your new cell. By default, it will only show the date and not the time. To make it show the time as well you need to change the cell format. With your new date cell selected, click the 123 format drop down menu and select the required cell format.

To apply this formula to all of the cells in the column, click the small black square (handle) at the bottom right of the date / time cell and drag it to the bottom of the column. Once you release the drag the columns should be populated with the date time conversion for its adjacent epoch data cell. You can see the epoch data cell index changed in the formula as you select each cell separately.

markhorrocks
  • 1,199
  • 19
  • 82
  • 151
  • Would you be able elaborate on where exactly my answer is incorrect? – ttarchala Feb 13 '18 at 16:30
  • Also, my answer is trivially easy to apply to an entire column with the use of =ARRAYFORMULA(). – ttarchala Feb 13 '18 at 16:31
  • 3
    I'm afraid too that it's your answer which is incorrect -- the OP is asking to convert the time in _seconds_, and so the time should be divided by 86,400 seconds per day. Why are you telling the OP to divide by 8,640,000? – ttarchala Feb 13 '18 at 16:34