Find Time Period Count Between Specific Dates - tsql

How can we find count of time period (etc. 12:00 PM - 01:00 PM) between two datetime values in T-Sql?
For example;
We choose "10/07/2015 12:00 AM" as start datetime (this value is parametrical) and we choose "10/09/2015 12:00 AM" as end datetime (this value is parametrical)
We want to learn count of time period (12:00 PM - 01:00 PM).
In this example;
10/07/2015 12:00 PM - 10/07/2015 01:00 PM (First)
10/08/2015 12:00 PM - 10/08/2015 01:00 PM (Second)
And we have to get "two".
Can you help about this?

declare #a datetime = '2015-01-01 12:00:00'
declare #b datetime = '2015-02-01 13:00:00'
select datediff(HOUR, #a, #b)
You can check the interval types you can use with datediff(...) function:
https://msdn.microsoft.com/en-us/library/ms189794.aspx

Related

How to plot a graph for specific day of every month for whole Year MATLAB

I have two variables called x and y. Each has 24*365 values. 24 presents the number of hours in a day and 365 presents the number of days in a year. I am plotting the values of 12th hour and 25th day by the following command:
plot(x(12:12,25), y(12:12,25))
Now I want to do the same for every 25th day for a whole year. Like 25th of Jan, 25th of Feb, 25th of March. I am not bothered about values of hours but I don't know how to create its logic as every month has different number of days.
You can generate the day of year number by getting the datenum values for the 25th of each month and subtracting the datenum of the 1st Jan that year.
dayIdx = datenum(2022,1:12,25) - datenum(2022,1,1) + 1;
Then just use this as your column index
plot(x(12,dayIdx), y(12,dayIdx))
The choice of 2022 above is arbitrary, as long as you pick a non-leapyear to get the 365-day year correct.
The datetime data type is awesome for this type of work.
%Make a vector of datetimes
ts = ( datetime(2001,1,1,0,0,0):hours(1):datetime(2001,12,31,023,0,0) )';
%Find the datetimes which are on the 25th day of the month, and the 12th
%hour of the day
mask = (day(ts) == 25) & (hour(ts) == 12) ;
%Confirm
ts(mask)
The result is below.
(You likely want to use the mask variable itself for your task. Sometimes you want to use find on the logical statement to get a list of indexes instead.)
ans =
12×1 datetime array
25-Jan-2001 12:00:00
25-Feb-2001 12:00:00
25-Mar-2001 12:00:00
25-Apr-2001 12:00:00
25-May-2001 12:00:00
25-Jun-2001 12:00:00
25-Jul-2001 12:00:00
25-Aug-2001 12:00:00
25-Sep-2001 12:00:00
25-Oct-2001 12:00:00
25-Nov-2001 12:00:00
25-Dec-2001 12:00:00

Converting the Time format in dateTime from 00:00:00 to 23:59:59

I have converted a Date into DateTime format, and it is returning me the hour format in 00:00:00 but I want it to be in 23:59:59
Date startDate = Date.newInstance(2021,2,1);
This returns the output as 2021-02-01 00:00:00
When I try to convert this to the 23:59:59 hour format by using the below code
DateTime startDateConvertTwo = DateTime.newInstance(startDate, Time.newInstance(23, 59, 59, 0));
It is pushing the date to next day and returning the value of 2021-02-02 07:59:59
I tried to sort this out by changing the values of Time.newInstance by adding it as Time.newInstance(15, 59, 59, 0) by doing which I get the expected result. But is it the right way to achieve what I am trying to do?
Please let me know if there are any other ways.
The returned output of Date startDate = Date.newInstance(2021,2,1); is not 2021-02-01 00:00:00. It's just a date with no information about time, but System.debug() display it as a DateTime, that's why you see 00:00:00.
Try System.debug(String.valueOf(startDate)); to see only the Date part.
DateTime.newInstance(date, time)
Constructs a DateTime from the specified date and time in the local time zone.
As documentation states, the DateTime you get is in your own time zone. Anyway System.debug() shows it in UTC time zone (GMT+0), so if your time zone is GMT-8 you'll see 2021-02-02 07:59:59.
System.debug(String.valueOf(startDateConvertTwo )); will shows the DateTime in your own time zone, so you'll see 2021-02-01 23:59:59.
If you need a DateTime in GMT you could use DateTime.newInstanceGmt(date, time):
DateTime startDateGMT = DateTime.newInstanceGmt(startDate, Time.newInstance(23, 59, 59, 0));
If you cannot use that method, you could add your offset to a DateTime:
public static DateTime toUTC(DateTime value) {
Integer offset = UserInfo.getTimezone().getOffset(value);
return value.addSeconds(offset/1000);
}
You could test it in anonymous console:
Date startDate = Date.newInstance(2021,2,1);
DateTime startDateConvertTwo = DateTime.newInstance(startDate, Time.newInstance(23, 59, 59, 0));
DateTime startDateGMT = DateTime.newInstanceGmt(startDate, Time.newInstance(23, 59, 59, 0));
DateTime startDateGMT2 = toUTC(startDateConvertTwo);
System.debug('startDateConvertTwo: ' + startDateConvertTwo); // startDateConvertTwo: 2021-02-01 22:59:59 // Because I'm at GMT+1
System.debug('String.valueOf(startDateConvertTwo): ' + String.valueOf(startDateConvertTwo)); // String.valueOf(startDateConvertTwo): 2021-02-01 23:59:59
System.debug('startDateGMT: ' + startDateGMT); // startDateGMT: 2021-02-01 23:59:59 // Now it's in UTC
System.debug('String.valueOf(startDateGMT): ' + String.valueOf(startDateGMT)); // String.valueOf(startDateGMT): 2021-02-02 00:59:59 // So in my locale time it's the day after,
System.debug('startDateGMT2: ' + startDateGMT2); // startDateGMT2: 2021-02-01 23:59:59 // Same as startDateGMT
System.debug('String.valueOf(startDateGMT2): ' + String.valueOf(startDateGMT2)); // String.valueOf(startDateGMT2): 2021-02-02 00:59:59
public static DateTime toUTC(DateTime value) {
Integer offset = UserInfo.getTimezone().getOffset(value);
return value.addSeconds(offset/1000);
}
The output of startDateGMT and startDateGMT2 will be the same.
Noteworthy: DateTime fields are stored in GMT. When shown in the standard Salesforce UI, they're converted to the user's timezone.

Convert text value ( 6/1/2018 11:34:33 PM , 6/2/2018 2:32:07 AM) to timestamp

I have a column with text datatype in PostgreSQL with values below:
6/1/2018 11:34:33 PM
6/2/2018 2:32:07 AM
I want to convert it to timestamp:
2018-01-06 23:34:333
2018-02-06 02:32:07
How do I do it?
I tried to_timestamp(entry_time_stamp, 'YYYY-MM-DD HH24:mi:ss') but it does not add PM value to it.
use :
to_timestamp(entry_time_stamp, 'YYYY-MM-DD HH12:mi:ss AM')

SharePoint custom list date and time validation

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
)

Converting non-standard date into SAS date in SAS

I have a date variable in the data. The value in the variable are not well formatted. I want to convert them into SAS date format like datetime9. .
date
Nov 2 2013 10:00AM
Oct 6 2012 11:00AM
Aug 26 2002 18:00PM
Mar 22 2012 07:00AM
There are a few functions to know about.
scan(str,n) -- this pulls the nth word from a string.
catt(str1,str2,str3) -- this concatenates strings.
input(str,informat) -- this converts a string to a number using the informat
dhms(d,h,m,s) -- Day Hour Minute Second. A date, plus time parts equals a datetime.
Also DDMONYYYY is informat = date9.
HH:MM <AM|PM> is informat = time.
So...
data test;
format date $20.;
date = "Nov 2 2013 10:00AM"; output;
date = "Oct 6 2012 11:00AM"; output;
date = "Aug 26 2002 18:00PM"; output;
date = "Mar 22 2012 07:00AM"; output;
run;
data test;
set test;
format day date9. time time. date2 datetime.;
day = input(catt(scan(date,2),scan(date,1),scan(date,3)),date9.);
time = input(scan(date,4),time.);
date2 = dhms(day,hour(time),minute(time),second(time));
run;
data any;
attrib datevar format=date9.;
infile cards;
input datevar ANYDTDTM20.;
datevar=datepart(datevar);
cards;
Nov 2 2013 10:00AM
Oct 6 2012 11:00AM
Aug 26 2002 18:00PM
Mar 22 2012 07:00AM
;run;