Subtract 1 year from datetime - asp.net

I have seen a lot of info on how to subtract one datetime from the other and how to add years on to a datetime. but the following is giving me a headache....
When a user inserts a record a hidden field called subdate with a value of datetime.now is added to the db.
I then need to have an 'old records' page that lists all the entries that are over 1 year old and was hoping to use something (but using subtract method) similar to;
(DateTime.Now.AddYears(1))
but there is no SubtractYears available? Why?
Please can you let me know how I achieve the same result?

DateTime.Now.AddYears(-1)
From the documentation:
A number of years. The value parameter can be negative or positive.

now = datetime.now()
last_year = (now.year - 1)
datestr = (datetime.strptime(str(now.year - 1), "%Y")).strftime("%y")
print(f"Last Year: {datestr}")
The output will be:
Last Year: 20
If you prefer to have four digit year then change %y to %Y

Related

How to add Two Columns (Date/Time) Together in Tableau

I have a dataset which was imported from Excel to Tableau. In Excel the data is listed as "8:15:00 AM". When it's imported into Tableau it's now Date & Time as "12/30/1899 10:45:00 AM".
What I'm trying to perform is an addition problem between two date & time columns. An example being:
Sleep: 12/30/1899 10:45:00
Eating: 12/30/1899 00:45:00
Sleep + Eating which should yield 10:40 + 00:45 = 11:30
After much googling and video watching, I have not found a solution.
Creating a calculated as such should solve the problem:
DATEADD(
'hour', DATEPART('hour', [Eating]), DATEADD(
'minute', DATEPART('minute', [Eating]), DATEADD(
'second', DATEPART('second', [Eating]), [Sleep])))
From there, the time display of the field can be adjusted as necessary:
Right-click field name > Default Properties > Date Format
You can achieve the desired result by creating three calculated fields. Lets call one of the calculated field [hours] and another one [minutes]. A third field will use the values from [hours] and [minutes] in order to properly display the results. Let's call this third field [Sleep + Eating].
To create [hours]:
//[hours] calculated field
DATEPART('hour', [Sleep])+ DATEPART('hour', [Eating])
To create [minutes]:
//[hours] calculated field
DATEPART('minute', [Sleep])+DATEPART('minute', [Eating])
At this point [hours] and [minutes] are just the sums of the integer values from [Sleep] and [Eating]. To display these sums in the format you have requested, you'll need to have a third calculated field that concatenates hours and the minutes, and is also able to handle a situations where the minutes add up to more than 60, or when the minutes add up to less then 10.[Sleep + Eating] calculated field will achieve this.
Here is the [Sleep + Eating] calculated field:
//[Sleep + Eating] calculated field
IF [minutes]< 10 THEN STR([hours])+":0"+STR([minutes])
ELSEIF [minutes]< 60 THEN STR([hours])+":"+STR([minutes])
ELSEIF [minutes]>= 70 THEN STR([hours]+1)+":"+STR([minutes]-60)
ELSE STR([hours]+1)+":0"+STR([minutes]-60)
END
Other solutions that try to treat your data like a date field (See #Daniel Sims answer) could be problematic if the hours sum to greater than 23, the result will add a day. For example, lets say you had these values in Excel:
If you use my solution, the result will be 25:02. If you use #Daniel Sims answer, the result would be 12/31/1899 1:02 :00 AM, which I don't think is what you want.

how to convert a string date variable into datetime variable in sas

I have a string variable for datetime. Sometimes it is a whole number like 3040000 sometimes a decimal value like this 3130215.123.
I would like to convert this into a date time variable like mm-dd-yyyy.
Thanks in advance.
add: I think the value 3130215.123 refers to feb-15-2013 12:30:00.
I think these are
3YYMMDD.HHMM
3130215.123 -> 3|13|02|15|12|30
(the last 0 is assumed).
So, you need:
length year month day hour minute second $2;
year=substr(dtvar,2,2);
month=substr(dtvar,4,2);
day=substr(dtvar,6,2);
hour=translate(subpad(dtvar,9,2),'0',' ');
minute=translate(subpad(dtvar,11,2),'0',' ');
second=translate(subpad(dtvar,13,2),'0',' ');
then
new_dtvar=dhms(mdy(month,day,year),hour,minute,second);
Dates, Times, and Datetimes are stored as doubles. Only the format matters for display.
try
var1 = input(var,best.);
format var1 datetime19.;
in a data step to apply the format. Then look at the results.
Based on comments on the original question, the statements would be:
format var_date ddmmyyd10. var_time time5. var_datetime datetime19.;
var_date=input(put(int(17000000+(var)),10.),yymmdd10.);
var_time=input(put((var-int(var))*1e6,z6.),hhmmss6.);
var_datetime=dhms(var_date,hour(var_time),minute(var_time),0);
Haven't had a chance to test, so feel free to comment with any errors you get.

How to add/subtract date/time components using a calculated interval?

I'd like to get this to work in Teradata:
Updated SQL for better example
select
case
when
current_date between
cast('03-10-2013' as date format 'mm-dd-yyyy') and
cast('11-03-2013' as date format 'mm-dd-yyyy')
then 4
else 5
end Offset,
(current_timestamp + interval Offset hour) GMT
However, I get an error of Expected something like a string or a Unicode character blah blah. It seems that you have to hardcode the interval like this:
select current_timestamp + interval '4' day
Yes, I know I hardcoded it in my first example, but that was only to demonstrate a calculated result.
If you must know, I am having to convert all dates and times in a few tables to GMT, but I have to account for daylight savings time. I am in Eastern, so I need to add 4 hours if the date is within the DST timeframe and add 5 hours otherwise.
I know I can just create separate update statements for each period and just change the value from a 4 to a 5 accordingly, but I want my query to be dynamic and smart.
Here's the solution:
select
case
when
current_date between
cast('03-10-2013' as date format 'mm-dd-yyyy') and
cast('11-03-2013' as date format 'mm-dd-yyyy')
then 4
else 5
end Offset,
(current_timestamp + cast(Offset as interval hour)) GMT
You have to actually cast the case statement's return value as an interval. I didn't even know interval types existed in Teradata. Thanks to this page for helping me along:
http://www.teradataforum.com/l081007a.htm
If I understand correctly, you want to multiply the interval by some number. Believe it or not, that's literally all you need to do:
select current_timestamp as right_now
, right_now + (interval '1' day) as same_time_tomorrow
, right_now + (2 * (interval '1' day)) as same_time_next_day
Intervals have always challenged me for some reason; I don't use them very often. But I've had this little example in my Teradata "cheat sheet" for quite a while.
Two remarks:
You could return an INTERVAL instead of an INT
The recommended way to write a date literal in Teradata is DATE 'YYYY-MM-DD' instead of CAST/FORMAT
select
case
when current_date between DATE '2013-03-10' and DATE '2013-11-03'
then interval '4' hour
else interval '5'hour
end AS Offset,
current_timestamp + Offset AS GMT

SQLite : obtain current week

I'm trying to obtain the current week for date comparison in SQLite.
I have no problem for last month, last year, today, yesterday... but don't find the solution to have the current week.
I tried lot of things like:
SELECT tastings.* FROM tastings
WHERE (DATE(tastings.date) > DATE('now','weekday 1','+ 7 days'))
Can you help me ? Thanks.
This code gives you the week number where the first day of week is monday. It also works well for last and first weeks of the year.
strftime('%W', 'now', 'localtime', 'weekday 0', '-6 days')
I guess you want compare 2 date, Assume you have a table named _testTbl and have 3 column _id INTEGER, _name TEXT, _recordDate TEXT
you want name that record this week
you can use below code:
SELECT * FROM _testTbl
WHERE _recordDate > datetime('now', 'start of day', 'weekday 6', '-7 day')
note that this week start by saturday (sunday 0, monday 1, ..., saturday 7)
this t-sql means:
datetime is a sqlite date and time function.
first parameter is given time: 'now' means the current time.
second parameter take the time to start of day.
third parameter take time to the next weekday number (in this case, saturday).
fourth parameter take time to start of week
What is stored inside the tastings.date column? Note that SQLite does not have “timestamp” type affinity, so probably you store Text (some representation of the date) or integer (julian day, epoch…)
All time and date functions expect a valid time string and convert that time string to another string format. If tastings.date contains a week number then use:
AND cast(tastings.date AS TEXT) = strftime('%W','now')
This helps me to compare the 2 dates using the week of the year.
AND ( strftime('%W', tastings.date) = strftime('%W', 'now') )
Thanks you.

DateAdd function in pl/sql

As the title suggested , I am looking for a function in pl /sql which does something similar like the DateAdd function.
I have been looking and I found the add_months function but I would really like one that is a little more variable since I need to be able to add minutes, hours , days etc.
It appears there's not many solutions :
PL/SQL allows you to perform arithmetic operations directly on date variables. You may add numbers to a date or subtract numbers from a date. To move a date one day in the future, simply add 1 to the date as shown below:
hire_date + 1
You can even add a fractional value to a date. For example, adding 1/24 to a date adds an hour to the time component of that value. Adding 1/(24*60) adds a single minute to the time component, and so on.
Besides adding numbers to dates - though it's the simplest way - you can add intervals like that:
date1 := date2 + interval '1' day;
date1 := date2 + interval '2' month;
date1 := date2 + interval '3' year;
It's almost the same but I prefer latter for better readability.

Resources