Unix time stamp to date and hour in google sheets - date

I am working in Google Sheets. How can I convert a timestamp in seconds (as type of 1634978274) to the format of 2021-10-23 08:23, e.g. Date, hours; Minutes and how can I store this result in a new column? The original timestamp as such should not be changed, though.
Unfortunately, I am not sure by which number to divide the timestamp as such to receive the correct date.
I strive to filter for the dates and hours, which is why I need to transform the timestamp in the first place.

if your unix / epoch time is in seconds use:
=TEXT(A2/86400+DATE(1970, 1, 1), "dd/mm/yyyy hh:mm:ss")
without seconds:
=TEXT(A2/86400+DATE(1970, 1, 1), "dd/mm/yyyy hh:mm")
if your unix / epoch time is in milliseconds use:
=TEXT(A2/86400000+DATE(1970, 1, 1), "dd/mm/yyyy hh:mm:ss.000")
for array use:
=INDEX(IF(A1:A="",,TEXT(A1:A/86400+DATE(1970, 1, 1), "dd/mm/yyyy hh:mm:ss")))

Unix time counts the number of elapsed seconds from an origin of January 1, 1970. So to convert to Sheets time:
=DATE(1970,1,1) + 1634978274/(60*60*24)
... where 60*60*24 = "60 seconds per minute x 60 minutes per hour x 24 hours per day."
Then you can format the formula cell (or range) with the Date/Time format of your choice.
If your Unix time will be entered into a cell, of course you can substitute 1634978274 in the formula with that cell reference, e.g.:
=DATE(1970,1,1) + A1/(24*60*60)

Related

Extract time from date time and find difference between 2 times

I am trying to convert EPOC time to date time and need to extract the time only from that
I am doing below
$min = $Time_Start | measure -Minimum
$max = $Time_End | measure -Maximum
[datetime]$oUNIXDatemin=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds($min.Minimum))
$oUNIXDatemin_1 = $oUNIXDatemin.ToString("HH:mm:ss")
[datetime]$oUNIXDatemax=(Get-Date 01.01.1970)+([System.TimeSpan]::fromseconds($max.Maximum))
$oUNIXDatemax_1 = $oUNIXDatemax.ToString("HH:mm:ss")
Problem is while converting I am getting $oUNIXDatemin_1 and $oUNIXDatemax_1 value like
$oUNIXDatemin_1
12 October 2021 07:46:46
$oUNIXDatemax_1
12 October 2021 21:16:04
My EPOC values are
$min.Minimum
1634024806
$max.Maximum
1634073364
Please let me know what is wrong here. Need to find the difference in HH:mm:ss format.
In PowerShell, you'd usually use a format string. Subtracting two PowerShell datetimes returns a value of type Timespan, which is well-behaved over a span of more than 24 hours.
([datetime]"12 October 2021 21:16:04" - [datetime]"12 October 2021 07:46:46") -f "HH:mm:ss"
13:29:18
Be careful here. Both intervals (durations) and time (of day) have the same format, but different meanings. For example, it makes sense to multiply the interval "01:00:00" (1 hour) by 3 to get three hours; it doesn't make sense to multiply the time "01:00:00" (1 o'clock AM) by 3.
I'm sure the overall calculation can be simplified, but it's too early for me.

Rounding seconds of time with HH:MM:SS format to nearest minute

For Example: Sunset-Sunrise.org provides sunset/sunrise time with HH:MM:SS format.
Given a time such as 12:53:57, I want to round the seconds to 12:54:00. Please advise.
A general technique for rounding is to add half of the unit you want to round to and then truncating. For example, if you want to round an integer to the nearest ten's digit, you can add 5 and discard the one's digit: ((x + 5) ~/ 10) * 10.
The same technique works for times too. You can first parse the HH:MM:SS string into a DateTime object. Then, to round the DateTime to the nearest minute, you can add 30 seconds and copy all of the resulting fields except for the seconds (and subseconds):
DateTime roundToMinute(DateTime dateTime) {
dateTime = dateTime.add(const Duration(seconds: 30));
return (dateTime.isUtc ? DateTime.utc : DateTime.new)(
dateTime.year,
dateTime.month,
dateTime.day,
dateTime.hour,
dateTime.minute,
);
}
You can use date_time_fromat packages
from the docs
final timeOffset = dateTime.subtract(Duration(hours: 6, minutes: 45));
// 7 hours
print(DateTimeFormat.relative(timeOffset));
// 6 hours
print(DateTimeFormat.relative(timeOffset, round: false));
This is the URL

How to convert formatted date to unix epoch in Libreoffice calc

I have column with cell format date or time (DD.MM.YYYY HH:MM:SS) and values like
03.12.2013 14:01:49
04.12.2013 10:19:27
04.12.2013 12:44:56
04.12.2013 14:20:12
04.12.2013 18:30:21
I need those values converted to unix epoch (seconds since 1970). Somehow it feels like the values are not recognized as dates, but rather as strings. I tried different formats, had little luck with dates without time.
Operations performed on date data should be automatic provided that the cells are formatted as as a user defined DD.MM.YYYY HH:MM:SS in the 'Format' > 'Cells' > 'Numbers' tab.
If you're using the standard settings, LibreOffice Calc uses 12/30/1899 as it's default date. So the first step is getting the number of days between 12/30/1899 and 1/1/1970:
=(DATE(1970,1,1) - DATE(1899,12,30)) = 25569
Number of seconds in a day:
=(60 * 60 * 24) = 86400
If, for example, in cell A2 you have the date 03.12.2013 14:01:49. I subtract the difference between Calc's default date and the Unix Epoch we just calculated, and multiply it by the number of seconds in a day:
=(A2 - 25569) * 86400
The result is a value of 1363096909 which is the Epoch time in seconds. If you need it in milliseconds, multiply the equation by 1000.
If it's something you use a lot, you can create a custom function that does this. Go to Tools > Macros > Edit Macros, and type out the following into whichever module comes up:
REM ***** BASIC *****
Function EPOCH(date_cell)
EPOCH = (date_cell - 25569)*86400
End Function
Close the macro IDE, and now you can use your EPOCH() like any other function!
This formula worked for me, where the others above did not:
= DATE( 1970, 1, 1 ) + ( A1 / 86400 )

Octave database package timestamp from PostgreSQL

Octave database package determines that PostgreSQL timestamp is returned in Octave as 8-byte-time-value positive or negative difference to 2000-01-01 00:00 (int64 scalar, representing microseconds)
e.g. timestamp 2015-12-04 11:22:08 is returned as 502543328000000 in Octave.
How do I convert this to more readable and usable form if I intend to plot time series in Octave?
Use datestr. Note that datestr expects the serial date number in units of days and since 1 Jan 0000 as seen from the datenum docs
Return the date/time input as a serial day number, with Jan 1, 0000
defined as day 1.
So first we would have to convert your PostgreSQL serial number to (a) units of days instead of microseconds and (b) to start from the year 0000 instead of 2000.
timestamp = 502543328000000; % in microsecond since 2000
timestamp = timestamp / (1000000*60*60*24); % Days since 2000
timestamp = timestamp + datenum([2000,1,1]); % Days since 0000
datestr(timestamp, "yyyy-MM-dd HH:mm:ss") % Formatted string
There is a problem with datestr format in previous answer, i.e. it returns months in place of minutes and minutes in place of months. Proper format is probably
timestamp = input_time; % in microsecond since 2000
timestamp = timestamp ./ (1000000*60*60*24); % Days since 2000
timestamp = timestamp + datenum([2000,1,1]); % Days since 0000
datestr(timestamp(i), "yyyy-mm-dd HH:MM:SS.FFF") % Formatted string
where FFF stands for miliseconds if needed.

Date Time Conversion: Number to Date Time

I have date & time in intervals of one hour(3600 seconds) in number format, e.g 0,3600, 7200, 10800, 14400, 18000 etc.
I have starting date and time , e.g 0 corresponds to 2005/06/01 01:00 in 'yyyy/mm/dd HH:MM' format.
I am writing this data to Excel file, so I am looking for way where I can convert time given in Hour (in seconds) to Date Time (2005/06/01 01:00, 2005/06/01 02:00 etc) before writing to excel file.
I have explored 'datenum' and 'datestr' functions but they are not useful since I can not give them customised start time i.e (0 corresponds to 2005/06/01 01:00).
May be if some one can help me to point me in right direction.
tempMatrix = [NrID time_inSec ff X Y];
tempMatrix_dataCell=num2cell(tempMatrix);
col_header={'NrID','Time','ff','X','Y'};
data_for_xls_file=[col_header; tempMatrix_dataCell];
xlswrite('My_file.xls',data_for_xls_file);
time_inSec is column with values 0, 3600, 7200, 10800 etc which need to be converted.
When I use datenum it returns 7.3246e+05 so when I add 3600 to get 2005/06/01 02:00 and pass it to datestr it returns 2015/04/10 01:00.
temp_time = datenum('2005/06/01 01:00','yyyy/mm/dd HH:MM')
This works with a given start time:
startTime = datenum('2005/06/01 01:00', 'yyyy/mm/dd HH:MM'); % Define start time.
currentTime = datenum('2005/06/01 02:00', 'yyyy/mm/dd HH:MM'); % Current time.
timePassedHours = (currentTime - startTime) * 24; % Time that has passed in hours.
display(timePassedHours); % Print the output.