I know it is very very simple but am not getting how to do it.
end_time = Time.new(Date.today.year, Date.today.month, Date.today.day, 8, 00, 00).strftime("%H:%M:%S")
But in above code i want tomorrow instead of today i.e. '+1 day'. How can i do it?
I tried:
end_time = Time.new(Date.today.year, Date.today.month, Date.today.day, 8, 00, 00).strftime("%H:%M:%S") + 1.day
or
end_time = Time.new(Date.tomorrow.year, Date.tomorrow.month, Date.tomorrow.day, 8, 00, 00).strftime("%H:%M:%S")
But no use. Can anybody answer it?
In rails you can use : end_time + 1.day or 1.day.from_now
Added specific case:
if #order.delievery_time.between?(start_time, end_time + 1.day)
#time = #order.delievery_time.strftime("%H:%M:%S")
end
if you want date in a date field
Simply use :-
= date_field_tag :date_of_journey, Date.tomorrow
or if you want to hold it in a variable use:-
date = Date.tomorrow
Related
A glitch in code on my website caused the start date and end date in an sql table to be set to the same day for my online member directory. I have the code glitch corrected, but now need a way to fix the data in the database. I am trying to add a certain number of months (12, 18, 24, etc) to the end_date column depending on the package column. I only want to do this when the start_date column is equal to the end_date column. I have a query started, but keep running into an invalid syntax message. Can someone help? My sql is below
UPDATE porye_jbusinessdirectory_orders
SET end_date = DATE_ADD(months,12,end_date)
WHERE start_date = end_date
AND package_id = 1;
SET end_date = DATE_ADD(start_date, INTERVAL 12 MONTH)
This is for MySQL.
If I understand you correctly, you want to use DATEADD instead.
Your syntax seems like DATEADD (datepart , number , date ) which is not the syntax for DATE_ADD
UPDATE porye_jbusinessdirectory_orders
SET end_date = DATEADD(month, 12 ,end_date)
WHERE start_date = end_date
AND package_id = 1;
Check more here
Here is our current string:
select *
from trailer
where trailer.company_id = 'TMS'
and (trailer.is_active = 'A' or trailer.is_active = 'S')
and (trailer.inspection_date >= {d '2019-03-26'})
Problem is the date - I need this to always be 335 days in the past based on todays date - so always >=(todays date -335)
Groovy Console version 2.0.1 - running it on a McLeod LoadMaster version 17.2.0
replace and (trailer.inspection_date >= {d '2019-03-26'}) with DateAdd(yyyy,-1,GetDate())
See if that gives you what you are expecting.
I am trying to create a datetime variable for the past 3 hours... by concatenating DATE variable (in DATE format) and time variable (string hh:mm:ss) within PROC SQL;
Would highly appreciate any help with this!
Example:
APPLCTN_DT = 05NOV2018:00:00:00.000
APPLCTN_TM = 20:04:57
I would like to create a numeric DATETIME field based on the above
Since it looks like your "date" variable is really a DATETIME variable with zero time you perhaps can just add the time part to it?
new_datetime = APPLCTN_DT + input(APPLCTN_TM,time8.);
Or just to be safe you could force the time part of your datetime value to be zero before adding the time part. Here are a couple of ways.
new_datetime = dhms(datepart(APPLCTN_DT),0,0,input(APPLCTN_TM,time8.));
new_datetime = intnx('dtdate',APPLCTN_DT,0) + input(APPLCTN_TM,time8.);
Presuming the _DT variable is actually a datetime value with no exact time portion (thus just the date)).
Use DATEPART to extract SAS date value, INPUT to convert time string to time value, DHMS to construct a target date time value and INTNX to compute a new date time value offset from the target.
data _null_;
APPLCTN_DT = '05NOV2018:00:00:00.000'dt ;
APPLCTN_TM = "20:04:57";
date_part = datepart(applctn_dt);
time_part = input(applctn_tm,time8.);
target_dt = dhms(date_part,0,0,0) + time_part;
target_minus_3hr_dt = intnx ('dthour'
, dhms(date_part,0,0,0) + time_part
, -3
);
target_minus_3hr_exact_dt = intnx ('dtsecond'
, dhms(date_part,0,0,0) + time_part
, -3*60*60
);
format target: datetime20.;
put target_dt ' combined';
put target_minus_3hr_dt ' combined, 3 hours ago';
put target_minus_3hr_exact_dt ' combine, exactly three hours ago (to the second)';
run;
Will show in log
05NOV2018:20:04:57 combined
05NOV2018:17:00:00 combined, 3 hours ago
05NOV2018:17:04:57 combine, exactly three hours ago (to the second)
Actually, your code is close to the result, i think you forgot to transfer SAS date format to display format.
using put function in variable target_minus_3hr_dt & target_minus_3hr_exact_dt,
since the SAS date is shown as numeric so we need to use put function to transfer.
data _null_;
APPLCTN_DT = '05NOV2018:00:00:00.000'dt ;
APPLCTN_TM = "20:04:57";
date_part = datepart(applctn_dt);
time_part = input(applctn_tm,time8.);
target_dt = put(date_part,date9.)||applctn_tm;
target_minus_3hr_dt = put((intnx ('dthour'
, dhms(date_part,0,0,0) + time_part
, -3
)),datetime20.);
target_minus_3hr_exact_dt = put((intnx ('dtsecond'
, dhms(date_part,0,0,0) + time_part
, -3*60*60
)),datetime20.);
put target_dt ' combined';
put target_minus_3hr_dt ' combined, 3 hours ago';
put target_minus_3hr_exact_dt ' combine, exactly three hours ago (to the second)';
run;
I am NEW to SQL Server coding, so please be kind.
I am trying to look at a column and if the time is after 11am then I need to add 1 day to the date and display the new date. If prior to 11am, it doesn't need to add a day, but instead just show the date itself.
As you can see in the picture the "FDP Date" is adding a day no matter what time it shows (IE:lines 3 & 4). As if the time is before 11am it show not add any time.
Please let me know if you can help.
Thank you in advance,
Brian
Code:
SELECT
[ReceiptDate],
[DeptRcptDate],
CASE
WHEN DeptRcptDate >= '11:00:00'
THEN DATEADD (DAY, 1, DeptRcptDate)
WHEN DeptRcptDate < '11:00:00'
THEN (DeptRcptDate)
ELSE 'Unknown'
END AS "FDP Date",
[OutcomeLtrDate],
CASE
WHEN OutcomeLtrDate >= '16:00:00'
THEN (OutcomeLtrDate) + 1
WHEN OutcomeLtrDate < '16:00:00'
THEN (OutcomeLtrDate)
ELSE 'Unknown'
END AS "LDP Date"
Picture of (same) code.
You can use a CASE statement with DATEPART
declare #date datetime = '20160801 11:01:00'
select
case
when datepart(hour,#date) >= 11
then dateadd(day,1,#date)
else #date
end
'11:00:00' is 11am of the zero date (January 1, 1753). Obviously all your dates are greater than that.
If you want to compare times only, cast to time first:
case when cast(DeptRcptDate as time(0)) >= '11:00:00' then dateadd(d, 1, DeptRcptDate)
You're checking a datetime against a string contaiing a time. I'm not surprised it's giving unexpected results.
Get the time portion of your datetime with datepart(), and compare that.
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017
I have a table invoices with this fields:
invDate -> a date field
invTime -> a time field
I need to do querys like
SELECT top 10 * from invoices WHERE DATETIME(invDate+invTime)
BETWEEN DATETIME('2013-12-17 17:58') AND DATETIME()
or something like that. I don't know how to concatenate the invDate and invTime to create a datetime field. The only thing that i could do is this horribly thing:
DATETIME( YEAR(invDate), MONTH(invDate), DAY(invDate), 17, 52 ) AS MyDatetime
Couldn't even get hour and time with hour(invTime) and minute(invTime):
DATETIME( YEAR(invDate), MONTH(invDate), DAY(invDate),
HOUR(invTime), MINUTE(invTime) ) AS MyDatetime
I'm doing the querys throught the VFP Odbc Driver via PHP.
You were pretty close. If the value coming from PHP is not of a date/time, how could VFP interpret it properly. VFP also has a function CTOT() (character to time), and expects it in the format of 'yyyy-mm-ddThh:MM:ss??'
yyyy = 4 digit year
mm = 1 OR 2 digit month
dd = 1 OR 2 digit day
T -- literally the letter "T"
hh = 1 OR 2 digit hour (but typical is 2 anyhow)
MM = 1 or 2 digit minute (but typical is 2)
ss = 1 or 2 digit for seconds -- not required
?? = "AM" or "PM" if you wanted to explicitly provide that vs 24 hour clock
The MM and ss are optional, so if you finished with "T1" would be 1:00:00am
Now, to finish your query.
WHERE DATETIME(invDate+invTime)
BETWEEN DATETIME('2013-12-17 17:58') AND DATETIME()
Since this appears to be querying all invoices between a given date/time and NOW (via DateTime()), you don't even need between, you can do
WHERE YourTable.Column > CTOT( '2013-12-17T17:58')
If you specifically DID have a date/time range to consider, THEN you could do something like
WHERE YourTable.Column BETWEEN CTOT( '2013-12-05T10:00') AND CTOT( '2013-12-14T11:58')
PROBLEMS WITH your DATE() and TIME() implementations
The problem is Date() is a function to either return current date, or create based on y/m/d provided such as date( 2013, 12, 7 ). If you are passing a string, use CTOD( 'mm/dd/yyyy' ) such as CTOD( 12, 7, 2013 ).
As for the TIME() function that just expects a number and is of no use for you. From the OleDbProvider, your best bet is to just create a php function that builds a single string in the CTOT() format I've described and pass to the php function the date and time fields. Then use that as your "CTOT( functionReturnResult )"
To add a Date and a Time Field together you will need to convert them both to a same datatype 1st and than just simply add them together something like this....
DECLARE #D DATE = '2013-12-17'
DECLARE #T TIME = '17:58:00'
SELECT CAST(#D AS DATETIME) + CAST(#T AS DATETIME)
Result
2013-12-17 17:58:00.000
Your Query
SELECT top 10 *
from invoices
WHERE CAST(invDate AS DATETIME) + CAST(invTime AS DATETIME)
BETWEEN '20131217 17:58:00.000' AND GETDATE()