I have a date-time variable called assign1_submission (type double and format %tchh:MM:SS_AM). I have another date-time variable time_due (type double and format %tchh:MM:SS_AM).
gen double time_due = clock("12:00 PM", "hm")
format time_due %tchh:MM:SS_AM
In the assign1_submission that I imported from MS Excel, the variable only contained a time. However, in the data browser, Stata attached a date to the entry.
Despite only telling Stata a time, my time_due variable has a date in the entry.
What I want to do is delete the date in both variables, because I want to see the time difference between time_due and assign1_submission.
So, I tried the following code:
replace assign1_submission = hh(assign1_submission)
replace time_due = hh(time_due)
gen time_difference = time_due - assign1_submission
According to Stata:
hh(e_tc)
Description: the hour corresponding to datetime e_tc (ms. since
01jan1960 00:00:00.000)
When running this code:
replace assign1_submission = hh(assign1_submission)
Stata disregards the previous time: everything is overwritten to state: 12:00 AM, where a date is still attached to the entry.
Can someone please help to delete the date in both the assign1_submission and time_due variables so that I can determine the difference in hours between the two variables?
Date-times in Stata are counted in milliseconds (ms) since the start of 1960. You can discard the date element by getting the remainder on division by 24 * 60 * 60000, the number of ms in 1 day.
The time concerned is still tacitly a time on 1 January 1960.
clear
set obs 1
gen double datetime = clock("4 July 2020 18:00:00", "DMY hms")
format datetime %tc
gen double time = mod(datetime, 24 * 60 * 60 * 1000)
format time %tcHH:MM:SS
l, noobs
+-------------------------------+
| datetime time |
|-------------------------------|
| 04jul2020 18:00:00 18:00:00 |
+-------------------------------+
Otherwise it's hard to follow your assertions. Stata doesn't change data because you look in the browser. What you are showing as daily dates pertains to different variables.
I tried to group my daily data by week (given a reference date) to generate a smaller panel data set.
I used postgres before and there it was quite easy:
CREATE TABLE videos_weekly AS SELECT channel_id,
CEIL(DATE_PART('day', observation_date - '2016-02-10')/7) AS week
FROM videos GROUP BY channel_id, week;
But it seems like it is not possible to subtract a timestamp with a date string in Drill. I found the AGE function, which returns an interval between two dates, but how to convert this into an integer (number of days or weeks)?
DATE_SUB may help you here. Following is an example:
SELECT extract(day from date_sub('2016-11-13', cast('2015-01-01' as timestamp)))/7 FROM (VALUES(1));
This will return number of weeks between 2015-01-01 and 2016-11-13.
Click here for documentation
Hi im currently using momentjs for my dates in my project and im having a bit of trouble when subtracting 2 dates.
Here are my sample dates:
2016-10-08 10:29:23
2016-10-08 11:06:55
ive tried using the diff and subtract from the guide docs of momentjs but i got nothing.
And what if the date subtracted dates are more than 24 hours?
Thnks in advance.
You are correct, you can use moment's diff function to subtract two dates (see my example on Plunker):
var date1 = moment('2016-10-08 10:29:23');
var date2 = moment('2016-10-08 11:06:55');
var diff = date2.diff(date1);
Diff will be equal to 2252000, the number of milliseconds between the two date. See documentation for more details.
You can pass a second argument to diff with the measurement to use (years, months, weeks, days, hours, minutes, and seconds), so if you want to know the number of minutes between the two dates you can write:
var diffInMinutes = date2.diff(date1, 'minutes');
And you get 37 minutes.
How can I accurately convert the products (units is in days) of the difftime below to years, months and days?
difftime(Sys.time(),"1931-04-10")
difftime(Sys.time(),"2012-04-10")
This does years and days but how could I include months?
yd.conv<-function(days, print=TRUE){
x<-days*0.00273790700698851
x2<-floor(x)
x3<-x-x2
x4<-floor(x3*365.25)
if (print) cat(x2,"years &",x4,"days\n")
invisible(c(x2, x4))
}
yd.conv(difftime(Sys.time(),"1931-04-10"))
yd.conv(difftime(Sys.time(),"2012-04-10"))
I'm not sure how to even define months either. Would 4 weeks be considered a month or the passing of the same month day. So for the later definition of a month if the initial date was 2012-01-10 and the current 2012-05-31 then we'd have 0 years, 5 months and 21 days. This works well but what if the original date was on the 31st of the month and the end date was on feb 28 would this be considered a month?
As I wrote this question the question itself evolved so I'd better clarify:
What would be the best (most logical approach) to defining months and then how to find diff time in years, months and days?
If you're doing something like
difftime(Sys.time(), someDate)
It comes as implied that you must know what someDate is. In that case, you can convert this to a POSIXct class object that gives you the ability to extract temporal information directly (package chron offers more methods, too). For instance
as.POSIXct(c(difftime(Sys.time(), someDate, units = "sec")), origin = someDate)
This will return your desired date object. If you have a timezone tz to feed into difftime, you can also pass that directly to the tz parameter in as.POSIXct.
Now that you have your date object, you can run things like months(.) and if you have chron you can do years(.) and days(.) (returns ordered factor).
From here, you could do more simple math on the difference of years, months, and days separately (converting to appropriate numeric representations). Of course, convert someDate to POSIXct will be required.
EDIT: On second thought, months(.) returns a character representation of the month, so that may not be efficient. At least, it'll require a little processing (not too difficult) to give a numeric representation.
R has not implemented these features out of ignorance. difftime objects are transitive. A 700 day difference on any arbitrary start-date can yield a differing number of years depending on whether there was a leap year or not. Similarly for months, they take between 28-31 days.
For research purposes, we use these units a lot (months and years) and pragmatically, we define a year as 365.25 days and a month as 365.25/12 = 30.4375 days.
To do arithmetic on a given difftime, you must convert this value to numeric using as.numeric(difftime.obj) which is, in default, days so R stops spouting off the units.
You can not simply convert a difftime to month, since the definition of months depends on the absolute time at which the difftime has started.
You'll need to know the start date or the end date to accurately tell the number of months.
You could then, e.g., calculate the number of months in the first year of your timespan, the number of month in the last your of the timespan, and add the number of years between times 12.
Hmm. I think the most sensible would be to look at the various units themselves. So compare the day of the month first, then compare the month of the year, then compare the year. At each point, you can introduce a carry to avoid negative values.
In other words, don't work with the product of difftime, but recode your own difftime.
In xquery, How to get 90 days before date from a given date?
Say given date is 30-03-2012.
I want to get the date which is 90 days before the given date. Leap year calculation should also not get missed.
I could not find any built in functions. There are add/substract methods, but it works only with two dates.
Any ideas?
You need to subtract a dayTimeDuration (90 days) from a date, i.e.
xs:date("2012-03-30") - xs:dayTimeDuration("P90D")