This question already has answers here:
Convert decimal day to HH:MM
(3 answers)
Closed 3 years ago.
I have a dataset that encodes a date-time into two separate variables. Normally, I'd just paste them together inside of an as.POSIXct and carry on. However, the date is provided as a string, and the time of day as a fraction of 24 hours - e.g., 12pm is 0.5, 9:30am is 0.1458333, etc.
It doesn't seem all that tricky to convert the fractional days into clock hours, but I'd prefer to use a pre-existing function if possible. Does something like that exist in base R? A package?
If it's any use, this is an Excel (xlsx) time field imported into R through RODBC.
EDIT
Oddly enough, upon revisiting this problem, the times are now read in as POSIXct. Not sure what to make of that.
The R News 4/1 Help Desk article has a section on reading Excel dates in R.
POSIXct values are simply the number of seconds since midnight GMT 1970-01-01. (So you need to pay attention to your offset from UTC.) You can use the date part and add the number of days times 24*3600 (as.Date(dtval) to your time value * 24*3600. Gabor pointed to the article in R News (which he wrote, thank you, Gabor.)
You didn't give an example of the string. If you are getting your date as a string, then as.Date(strDate) will convert a variable "strDate" to Date class when it is in either "YYYY-MM-DD" or "YYYY/MM/DD" format. Otherwise the formatting codes are on the ?strptime page.
Once you have a POSIXct-classed variable you can just add the number of seconds. This example add 30 minutes to midnight today Feb 1, 2011 (in my time zone which is UTC-5):
> as.POSIXct(as.Date("2011-02-01")) +30*60
[1] "2011-01-31 19:30:00 EST"
And this is your time value added to midnight my time:
> as.POSIXct(as.Date("2011-02-01 00:00", tzone="UTC"))+3600*5 + 3600*24*timeval
[1] "2011-02-01 03:29:59 EST"
Related
I'm using manipulating time in excel.
I have the date in minutes I want to convert it into the format.
YY MM DD HH mm
where:
mm:Minutes
HH:Hours
DD:Days
MM:month
YY: year
Im using the following logic:
For 124 minutes I have
124/60= 2 hours
124%60 = 4 minutes
and so on for the days and the months and the years
The units Im using are:
mm-> HH /24
HH-> MM /30
MM-> YY /12
But something is really wrong:
Does anyone have an idea what was happening?
Thanks a lot, guys
Your calculations are "really wrong" because in real life, months have anywhere between 28 and 31 days, so you cannot represent a large number of minutes as a "date" that uses variable measures for years and months.
On the assumption that as units of measure you want to use ...
Year = 360 days
month - 30 days
... you can calculate as follows:
That's also what your calculation shows. If something is "really wrong" with that, it's probably your expectation, which is based on the traditional 365.25 day year.
Maybe you want to edit your question and explain what you expect as the "really correct" result.
In Excel dates are fractional number of days from 31-Dec-1899 (date 0 is formatted as 0/1/1900). So why don't you just add your minutes (after dividing by (24*60) to convert to days) to the earliest valid date in EXCEL (1900-01-01) and use the TEXT function to format:
=DATEVALUE("1900-01-01")+(21/24/60)-1
The bold part is your minutes.
This is now a proper EXCEL date; you can use TEXT function to format it into any way you like, or you can use DAY(), HOUR(), etc. functions to extract parts.
Having said that I don't know what your minutes represent; What date does Full Time in Minutes:21 correspond to?
I have a feeling this is easy but I just can't crack it and am spending too much time on it. I am trying to convert w2037.4 09:00 to a date time.
I ultimately would like to have the above be 09/10/2020 09:00.
I've tried ParseDate(RXReplace([value],"w"," ","i"),"yyww.d HH:mm") but this is definitely not it.
Any help is appreciated, thanks.
I hope this helps, even if it does not provide a complete answer.
I think the date you are looking for is 10th September (and not 9th October, as I had initially thought - please remember to specify date formats as they vary across countries).
From my understanding, your original column is made of
a w character
last two digits of year 2020
week 37
day of week 4
then the time portion
I cannot find in Spotfire a function that gives you the date from week and day of week. Can you use a TERR expression?
This one worked for me for the specific example you gave, but it is not bullet proof - weeks and days of week are tricky as they depend on your local/regional settings. In my case, I subtracted one day to make it work but you probably don't want it. Also, open source R and TERR give different results with week formats.
So the TERR Expression function I used is:
mydatetime=sapply(input1,function(x) sub('w','',x)) #remove the w
turnToDate = function(x) {
x.vector=strsplit(x,' ')[[1]] #separate date and time parts
x.date=x.vector[1] #store date portion
#is this correct? Remove if not!
x.date=as.character(as.numeric(x.date)+.1) #add a day
x.time=x.vector[2] #store time portion
y.date=as.Date(x.date,format = "%y%W.%w",tz='GMT') #convert week to date
y.datetime=paste(y.date,x.time) #add time as string
return (as.POSIXct(y.datetime,origin="1970-01-01 00:00:00"))
}
#do not use sapply as dates turn to numbers
output=Reduce(c,lapply(mydatetime,turnToDate))
I created it (from the Data>Data Function properties>Expression Functions menu) with the name TERR_convert, as a column function returning a DateTime. Then created a calculated column as :
TERR_convert([value])
I have a date with hours and minutes like 2/3/2019 10:30 am , and I do the following to find the diffence in hours which I already formatted like ( h )
=now() - date
I want to check if the difference is more than 12 ( where I mean 12 hours) to do something.
if(actual difference>12; "hello"; "bye")
But is not checking the actual difference but the serial difference
How can I check the actual difference in hours?
Dates are stored as numbers where the integer part is the number of days since 1899-12-31. The fractional part measures the time within a day. So, 24 * (now() - date) will give you the number of hours elapsed.
NB: the formatting is only a question of display and won't influence what's done by the formulas.
In order to check conditionaly the difference in the hours unit between two times we can do if(hour(actual difference)>12; "hello"; "bye")
We put the function hour() as noted in
https://support.office.com/en-us/article/calculate-the-difference-between-two-times-e1c78778-749b-49a3-b13e-737715505ff6
Trying to convert 1504865618099.00 Unix time into a readable date time.
I tried this:
=(UNIX + ("1/1/1970"-"1/1/1900"+1)*86400) / 86400
But it's not working.
To convert the epoch(Unix-Time) to regular time like for the below timestamp
Ex: 1517577336206
First convert the value with the following function like below
=LEFT(A1,10) & "." & RIGHT(A1,3)
The output will be like below
Ex: 1517577336.206
Now Add the formula like below
=(((B1/60)/60)/24)+DATE(1970,1,1)
Now format the cell like below or required format(Custom format)
m/d/yyyy h:mm:ss.000
Now example time comes like
2/2/2018 13:15:36.206
The three zeros are for milliseconds
=A1/(24*60*60) + DATE(1970;1;1) should work with seconds.
=(A1/86400/1000)+25569 if your time is in milliseconds, so dividing by 1000 gives use the correct date
Don't forget to set the type to Date on your output cell. I tried it with this date: 1504865618099 which is equal to 8-09-17 10:13.
TLDR
=(A1/86400)+25569
...and the format of the cell should be date.
If it doesn't work for you
If you get a number you forgot to format the output cell as a date.
If you get ##### you probably don't have a real Unix time. Check your
timestamps in https://www.epochconverter.com/. Try to divide your input by 10, 100, 1000 or 10000**
You work with timestamps outside Excel's (very extended) limits.
You didn't replace A1 with the cell containing the timestamp ;-p
Explanation
Unix system represent a point in time as a number. Specifically the number of seconds* since a zero-time called the Unix epoch which is 1/1/1970 00:00 UTC/GMT. This number of seconds is called "Unix timestamp" or "Unix time" or "POSIX time" or just "timestamp" and sometimes (confusingly) "Unix epoch".
In the case of Excel they chose a different zero-time and step (because who wouldn't like variety in technical details?). So Excel counts days since 24 hours before 1/1/1900 UTC/GMT. So 25569 corresponds to 1/1/1970 00:00 UTC/GMT and 25570 to 2/1/1970 00:00.
Now if you also note that we have 86400 seconds per day (24 hours x60 minutes x60 seconds) and you will understand what this formula does: A1/86400 converts seconds to days and +25569 adjusts for the offset between what is zero-time for Unix and what is zero-time for Excel.
By the way DATE(1970,1,1) will helpfully return 25569 for you in case you forget all this so a more "self-documenting" way to write our formula is:
=A1/(24*60*60) + DATE(1970,1,1)
P.S.: All these were already present in other answers and comments just not laid out as I like them and I don't feel it's OK to edit the hell out of another answer.
*: that's almost correct because you should not count leap seconds
**: E.g. in the case of this question the number was milliseconds since the the Unix epoch.
If you have ########, it can help you:
=((A1/1000+1*3600)/86400+25569)
+1*3600 is GTM+1
in case the above does not work for you. for me this did not for some reasons;
the UNIX numbers i am working on are from the Mozilla place.sqlite dates.
to make it work : i splitted the UNIX cells into two cells : one of the first 10 numbers (the date) and the other 4 numbers left (the seconds i believe)
Then i used this formula, =(A1/86400)+25569 where A1 contains the cell with the first 10 number; and it worked
You are seeing the date as ######## most likely because by definition the EPOCH times is in seconds - https://en.wikipedia.org/wiki/Unix_time. This means the number should be 10 characters long. Your number has 13 characters (see 1504865618099) and it is most likely in milliseconds (MS). In order to fix the formula just divide the number by 1000. Just keep in mind this way you'll loose the MS precision, but in most cases this is OK. So the final formula should be:
=A1/(86400 * 1000) + DATE(1970,1,1)
Just point and shoot.
Replace the C2 with your cell no. No need to format your Excel cell.
Also, you can use this unixtimestamp website to verify your data.
International format (ISO 8601):
=TEXT(C2/(1000*60*60*24)+25569,"YYYY-MM-DD HH:MM:SS")
2022-10-20 00:04:22
2022-10-20 00:05:20
2022-10-20 00:14:58
US format:
=TEXT(C2/(1000*60*60*24)+25569,"MM/DD/YYYY HH:MM:SS")
10/20/2022 00:04:22
10/20/2022 00:05:20
10/20/2022 00:14:58
Europe format:
=TEXT(C2/(1000*60*60*24)+25569,"DD.MM.YYYY HH:MM:SS")
20.10.2022 00:04:22
20.10.2022 00:05:20
20.10.2022 00:14:58
If you only need the date, remove the 'HH:MM:SS'.
=TEXT(C2/(1000*60*60*24)+25569,"YYYY-MM-DD")
I am doing some work in Excel and am running into a bit of a problem. The instruments I am working with save the date and the time of the measurements and I can read this data into Excel with the following format:
A B
1 Date: Time:
2 12/11/12 2:36:25
3 12/12/12 1:46:14
What I am looking to do is find the difference in the two date/time stamps in mins so that I can create a decay curve from the data. So In Excel, I am looking to Make this (if the number of mins in this example is wrong I just calculated it by hand quickly):
A B C
1 Date: Time: Time Elapsed (Minutes)
2 12/11/12 2:36:25 -
3 12/12/12 1:46:14 1436.82
I Have looked around for a bit and found several methods for the difference in time but they always assume that the dates are the same. I exaggerated the time between my measurements some but that roll over of days is what is causing me grief. Any suggestions or hints as to how to go about this would be great. Even If I could find the difference between the date and times in hrs or days in a decimal format, I could just multiple by a constant to get my answer. Please note, I do have experience with programming and Excel but please explain in details. I sometimes get lost in steps.
time and date are both stored as numerical, decimal values (floating point actually). Dates are the whole numbers and time is the decimal part (1/24 = 1 hour, 1/24*1/60 is one minute etc...)
Date-time difference is calculated as:
date2-date1
time2-time1
which will give you the answer in days, now multiply by 24 (hours in day) and then by 60 (minutes in hour) and you are there:
time elapsed = ((date2-date1) + (time2-time1)) * 24 * 60
or
C3 = ((A3-A2)+(B3-B2))*24*60
To add a bit more perspective, Excel stores date and times as serial numbers.
Here is a Reference material to read up.
I would suggest you to use the following:
Combine date to it's time and then do the difference. So it will not cause you any issues of next day or anything.
Please refer to the image with calculations. You may leave your total minutes cell as general or number format.
MS EXCEL Article: Calculate the difference between two times
Example as per this article
Neat way to do this is:
=MOD(end-start,1)*24
where start and end are formatted as "09:00" and "17:00"
Midnight shift
If start and end time are on the same day the MOD function does not affect anything. If the end time crosses midnight, and the end is earlier then start (say you start 23PM and finish 1AM, so result is 2 hours), the MOD function flips the sign of the difference.
Note that this formula calculates the difference between two times (actually two dates) as decimal value. If you want to see the result as time, display the result as time (ctrl+shift+2).
https://exceljet.net/formula/time-difference-in-hours-as-decimal-value
get n day between two dates, by using days360 function =days360(dateA,dateB)
find minute with this formula using timeA as reference =(timeB-$timeA+n*"24:00")*1440
voila you get minutes between two time and dates
I think =TEXT(<cellA> - <cellB>; "[h]:mm:ss") is a more concise answer. This way, you can have your column as a datetime.