How do I subtract 30 days from a date in PowerBuilder?
I have the following code that returns today's date in a parameter, but I need today - 30 days:
dw_1.setitem(1, "begin_datetime",
datetime(today(), Now()))
you're probably looking for the RelativeDate function. Unfortunately, it takes a Date and not a DateTime as a parameter, but we can get around that:
dw_1.setItem(1, "begin_datetime", DateTime( RelativeDate( today(), -30), Now() )
Related
I'm trying to manipulate a date value to go back in time exactly 1 ISO-8601 year.
The following does not work, but best describes what I want to accomplish:
date_add(date '2018-01-03', interval -1 isoyear)
I tried string conversion as an intermediate step, but that doesn't work either:
select parse_date('%G%V%u',safe_cast(safe_cast(format_date('%G%V%u',date '2018-01-03') as int64)-1000 as string))
The error provided for the last one is "Failed to parse input string "2017013"". I don't understand why, this should always resolve to a unique date value.
Is there another way in which I can subtract an ISO year from a date?
This gives the corresponding day of the previous ISO year by subtracting the appropriate number of weeks from the date. I based the calculation on the description of weeks per year from the Wikipedia page:
CREATE TEMP FUNCTION IsLongYear(d DATE) AS (
-- Year starting on Thursday
EXTRACT(DAYOFWEEK FROM DATE_TRUNC(d, YEAR)) = 5 OR
-- Leap year starting on Wednesday
(EXTRACT(DAY FROM DATE_ADD(DATE(EXTRACT(YEAR FROM d), 2, 28), INTERVAL 1 DAY)) = 29
AND EXTRACT(DAYOFWEEK FROM DATE_TRUNC(d, YEAR)) = 4)
);
CREATE TEMP FUNCTION PreviousIsoYear(d DATE) AS (
DATE_SUB(d, INTERVAL IF(IsLongYear(d), 53, 52) WEEK)
);
SELECT PreviousIsoYear('2018-01-03');
This returns 2017-01-04, which is the third day of the 2017 ISO year. 2018-01-03 is the third day of the 2018 ISO year.
I am trying to use the so called DateDiff function to subtract the End Date from the Start Date and obtain the numbers of days apart.For example:
10/11/1995 - 7/11/1995 = 3 (extract the 'dd' from DD/MM/YYYY format)
As date values are double with the integer part counting for a day, you can use this simple expression:
[Due Date]-[Start Date]
or, for integer days only:
Fix([Due Date]-[Start Date])
That said, you should a query for tasks like this.
I try to understand why
print(pd.Timestamp("2015-01-01") - pd.DateOffset(day=1))
does not result in
pd.Timestamp("2014-12-31")
I am using Pandas 0.18. I run within the CET timezone.
You can check pandas.tseries.offsets.DateOffset:
*kwds
Temporal parameter that add to or replace the offset value.
Parameters that add to the offset (like Timedelta):
years
months
weeks
days
hours
minutes
seconds
microseconds
nanoseconds
Parameters that replace the offset value:
year
month
day
weekday
hour
minute
second
microsecond
nanosecond
print(pd.Timestamp("2015-01-01") - pd.DateOffset(days=1))
2014-12-31 00:00:00
Another solution:
print(pd.Timestamp("2015-01-01") - pd.offsets.Day(1))
2014-12-31 00:00:00
Also it is possible to subtract Timedelta:
print(pd.Timestamp("2015-01-01") - pd.Timedelta(1, unit='d'))
pd.DateOffset(day=1) works (ie no error is raised) because "day" is a valid parameter, as is "days".
Look at the below one: "day" resets the actual day, "days" adds to the original day.
pd.Timestamp("2019-12-25") + pd.DateOffset(day=1)
Timestamp('2019-12-01 00:00:00')
pd.Timestamp("2019-12-25") + pd.DateOffset(days=1)
Timestamp('2019-12-26 00:00:00')
Day(d) and DateOffset(days=d) do not behave exactly the same when used on timestamps with timezone information (at least on pandas 0.18.0). It looks like DateOffset add 1 day while keeping the hour information while Day adds just 24 hours of elapsed time.
>>> # 30/10/2016 02:00+02:00 is the hour before the DST change
>>> print(pd.Timestamp("2016-10-30 02:00+02:00", tz="Europe/Brussels") + pd.offsets.Day(1))
2016-10-31 01:00:00+01:00
>>> print(pd.Timestamp("2016-10-30 02:00+02:00", tz="Europe/Brussels") + pd.DateOffset(days=1))
2016-10-31 02:00:00+01:00
I have a custom list that I'm trying to restrict data entry for valid day of week and time.
My current column validation works for day of week being Monday, Wednesday or Friday. It looks like this:
=CHOOSE(WEEKDAY([Requested date for approval]),FALSE,TRUE,FALSE,TRUE,FALSE,TRUE,FALSE)
I'm trying to figure out the syntax to add that it also has to be between 8 am and 12:00 pm on those days.
Any help would be greatly appreciated.
You would use an AND statement to include a second criteria
=AND(CHOOSE(WEEKDAY([Requested date for approval]),FALSE,TRUE,FALSE,TRUE,FALSE,TRUE,FALSE),
AND(
[Requested date for approval]-INT([Requested date for approval])*24 >= 8,
[Requested date for approval]-INT([Requested date for approval])*24 <= 24
)
)
I confess, I've never heard of the CHOOSE function, but the time calculation is based on the information at Microsoft
Convert times
To convert hours from the standard time format to a decimal number, use the INT
function.
Column1 Formula Description (possible result)
10:35 AM =([Column1]-INT([Column1]))*24 Number of hours since 12:00 AM (10.583333)
12:15 PM =([Column1]-INT([Column1]))*24 Number of hours since 12:00 AM (12.25)
EDIT
To calculate the day of the week, you can use the TEXT function to return the day of the week (i.e. Monday)
=TEXT(WEEKDAY([ColumnName]), "dddd")
It won't be pretty, but you can use a series of AND logical operators
=AND(
TEXT(WEEKDAY([Requested date for approval]), "dddd") = "Monday",
AND(
TEXT(WEEKDAY([Requested date for approval]), "dddd") = "Wednesday",
AND(
TEXT(WEEKDAY([Requested date for approval]), "dddd") = "Friday",
AND(
[Requested date for approval]-INT([Requested date for approval])*24 >= 8,
[Requested date for approval]-INT([Requested date for approval])*24 <= 24
)
)
)
)
Posting Working Solution
=IF(
AND(
CHOOSE(
WEEKDAY([Requested date for approval]),FALSE,TRUE,FALSE,TRUE,FALSE,TRUE,FALSE
),
([Requested date for approval]-INT([Requested date for approval]))*24>=8,
([Requested date for approval]-INT([Requested date for approval]))*24<=12
),
TRUE
)
let's say that I have a SmallDateTime column in my table. How to update hours in each row in T-SQL ?
Get the target value's hour part.
Find the difference between the hour you want and the found hour.
Add the difference of hours to the target value.
The script:
UPDATE atable
SET datetimevalue = DATEADD(hour, #hour - DATEPART(hour, datetimevalue),
datetimevalue)
WHERE ...
UPDATE YourTable SET YourDateColumn = DateAdd(hh, 1, YourDateColumn)
will add 1 hour to the time value in YourDateColumn in every single row.
See this page for more info.
UPDATE YourTableName
SET YourSmallDateTimeColumn = DATEADD(HH, 1, YourSmallDateTimeColumn)
Will add any number of hours to your column. So if you get rid of the time component first:
SELECT CAST(CONVERT(char(8), YourSmallDateTimeColumn, 112) AS smalldatetime)
and add hour component to it later it should work.
From here
To add or subtract hours to a datetime
or smalldatetime value, you will use
the DATEADD date function. The
DATEADD date function returns a new
datetime value based on adding an
interval to the specified date. The
syntax of the DATEADD date function is
as follows:
DATEADD ( datepart , number, date )
datepart is the parameter that
specifies on which part of the date to
return a new value. For hours, you
can use either HOUR or HH. number is
the value used to increment datepart.
date is an expression that returns a
datetime or smalldatetime value, or a
character string in a date format.
Here's an example on how to use the
DATEADD date function to increase or
decrease a datetime value by a certain
number of hours:
SELECT DATEADD(HOUR, -12, GETDATE())AS [TwelveHoursAgo]
SELECT DATEADD(HH, 6, GETDATE()) AS [SixHoursLater]