How do I get a string value of the 5 digit serial date format from a cell into my script? - string

I have a formula in a cell that is doing a vlookup to another sheet by combining the values of two cells on my spreadsheet. It's a table of names on the left and dates up top. I'm combining the name and date values into a single string value and using that as by vlookup key value, which works great. Now I need to basically do the same thing in my google script, but the date value has me at a loss. In the vlookup cell formula, the date value is the 5 digit date serial code. However when I combine the two cell values in my script, I cannot get the same 5 digit serial code out of the cell. I have tried all conversions of number, string, text, ect. What function can I use to get this 5 digit date value as a string in my script?
function serialDatevalue(){
var WB = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var NewDate = new Date();
var NewDate = WB.getRange('B1').getValue();
var JSdate = WB.getRange('B1').getValue();
Browser.msgBox(Number(NewDate));
// Some others that I have tried
// var JSdate = Date.parse(DateValue);
// var JSdate = Number(DateValue);
// var JSdate = Utilities.formatDate(DateValue, 'PST', '%');
}
for example, April 13, 2019 = 43568. I want to get this 5 digit value as a string.
I keep getting 'Sat Apr 13 2019 00:00:00 GMT-0700 (PDT)', or other longer integer values.
Please help.

From Tanaike's answer to Getting the number equalent of duration using GAS
var serialNumber = (dateObject.getTime() / 1000 / 86400) + 25569; // Reference: https://stackoverflow.com/a/6154953
The easier way to get the serialized number corresponding to a certain cell date value in Google Sheets is by using a the built-in Google Sheets function [TO_PURE_NUMBER][1]. This because Google Sheets and JavaScript, which is used by Google Apps Script, used different epoch (reference date used as 0 for the serialized numbers) and use different time units.
From https://developers.google.com/sheets/api/guides/concepts#datetime_serial_numbers
Date/Time serial numbers
Google Sheets, like most other spreadsheet
applications, treats date/time values as decimal values. This lets you
perform arithmetic on them in formulas, so you can increment days or
weeks, add or subtract two date/times, and perform other similar
operations.
Google Sheets uses a form of epoch date that is commonly used in
spreadsheets. The whole number portion of the value (left of the
decimal) counts the days since December 30th 1899. The fractional
portion (right of the decimal) counts the time as a fraction of one
day. For example, January 1st 1900 at noon would be 2.5, 2 because
it's two days after December 30th, 1899, and .5 because noon is half a
day. February 1st 1900 at 3pm would be 33.625.
Note that Google Sheets correctly treats the year 1900 as a common
year, not a leap year.
By the other hand, JavaScript use January 1, 1970 00:00 UTC as the epoch date and use milliseconds as the unit.
A word of caution: Custom code to make serialized number conversions from one to the other should consider if the spreadsheet and the Google Apps Script are using the same timezone or not.
Related
How to read value of fetched cell data as date google sheets API
Getting the number equalent of duration using GAS

Related

Excel formula to calculate elapsed minutes between 2 date timestamps only counting minutes during work hours

I have an excel sheet with about 50,000 records where I need to find the number of minutes between two date timestamps but I need to exclude any minutes that occurred during the times we are not working.
Our schedule is M-F 8:30am-5:30pm, Saturdays 8:30am-1:30pm
We don't work Sundays or holidays.
As an example
Cell B2: [7/3/2020 2:16:21 PM]
Cell C2: [7/6/2020 9:20:23 AM]
The manually calculated answer for this one should be about 244 minutes. Task started Friday afternoon, Saturday was a holiday, don't work Sundays, task completed at 9:20am on Monday.
Usually, I come here and start writing a question and by the time I've understood my own problem well enough to post a question I have figured it out on my own but not this time! Help!
Update:
#ForwardEd shared this...
=((I2-H2)
-MAX(0,(NETWORKDAYS.INTL(H2,I2,"0000011",$M$2:$M$12)-1+(WEEKDAY(I2,1)=7)))*TIME(15,0,0)
-MAX(0,(NETWORKDAYS.INTL(H2,I2,"1111101",$M$2:$M$12)-(WEEKDAY(I2,1)=7)))*TIME(19,0,0)
-NETWORKDAYS.INTL(H2,I2,"1111110",$M$2:$M$12)-(NETWORKDAYS.INTL(H2,I2,"0000000")
-NETWORKDAYS.INTL(H2,I2,"0000000",$M$2:$M$12)))*24*60
Where H:H is the Start Date Timestamp and I:I is the Response Date Timestamp and M2:M12 contains my holiday list.
It worked beautifully until I ran into an example like this:
H2 - 07/26/2020 7:48:45 PM
I2 - 07/27/2020 8:57:58 AM
The net result was -650.78333. It looks like anything that starts one one day and ends on the next is coming back as negative.
We want to measure the average response time in minutes for the applications that require manual underwriting. These start timestamps are times that loan applications were received online so they could come in any time of day. The stop times are timestamps that represent the system recorded response time. i.e. the timestamp where an underwriter first did something with the loan application. If a loan application was received at 7pm and was not auto-decisioned then a manual underwriter will need to do something with it the next day when we start working.
If that application came in at 7pm on Wed and is decisioned by an underwriter at 8:46am on Tuursday, we would want to document 16 minutes for that application - not 826 counting the hours between 7pm and 8:30am.
What you want to look at is NETWORKDAYS.INTL. Use this in conjunction with the custom settings to determine the number of Saturdays, Sundays and for the number of days in between your start and end time. You know you have X amount of time per day that is non working time, and Y amount per Saturday.
Then you formula in essence becomes
(End time - start time) - X * No. Weekdays - Y * No. Saturdays - No. Sundays - No. Holidays
Now there will be some tricks in there in order to count your days. but that is the gist of what it boils down to in a formula.
The formulas that are doing the brunt of the work are:
WORKDAY
NETWORKDAYS.INTL
TIME
I avoided the use of an if statement by using a boolean operation that excel will resolve from TRUE/FALSE to 1/0 when sent through a math operator. Side note: I read somewhere that this is also faster than an IF statement, but have no way of proving it and really does not matter on a small number of calculations.
WORKDAY
This formula will return the day of the week for a given date, and a set day of the week to be 1. It will be need in this solution to determine if the end date is a Saturday which has a value of 7 in default setup up as well when option 1 is picked. The format for the formula is:
WORKDAY(Excel Serial date, day 1 of the week)
For this solution
WEEKDAY(B3,1)
NETWORKDAYS.INTL
This formula will be used to count the number of specific days a start and an end date. It can exclude a custom weekend or count a custom week. If it is supplied with a list of dates that are holidays they can be excluded as well. The basic format of the formula is:
NETWORKDAYS.INTL(Start Date, End Date, Custom week choice or workweek pattern, range of holiday dates)
When entering the formula it will give you a list of predefined options for the weekend choices. It will not talk about the pattern.
The pattern is a string 7 digits long consisting of 1 or 0. 0's represents the days you want to count and 1's are days you want to ignore. An important part of the pattern is that the first entry is MONDAY. "1010111" would count only Tuesdays and Thursdays.
TIME
Excel stores date as an integer. 1 represents 1st of January 1900, 2 the 2nd of January 1900 and so on. Time is stored as a decimal or if you prefer the percentage/fraction of a day or 24 hour period. So rather than figuring out the math to determine what percentage of a day X number of hours is, it is simpler to let excel calculate it for us and make the number a little more understandable to someone who may be deciphering the formula later. The basic format of the formula is:
TIME(Hours, Minutes, Seconds)
So as stated earlier, 6 key components need to be determined:
X - Amount of non working time after a weekday
Y - Amount of non working time after a Saturday
Number of weekdays
Number of Saturdays
Number of Sundays
Number of holidays
1) Determine Weekday Non-Working Hours
Based on the supplied information that work day stops at 1730 and starts as 0830. There are a couple of ways of doing the math. Subtract the working hours from 24 hours or count the non work hours at the end of the day and add them to the non work hours at the start of the day.
24 - (17.5 - 8.5) = 15
or
(24 - 17.5) + (8.5 - 0) = 15
For this example 15 will be hard coded into the final formula
2) Determine Saturday Non-Working Hours
Similar to above. Note that we are ignoring Sunday as it is a designated non working day which we already know is 24 hours or 1 day. We are just interested in the time between end of shift Saturday and start of the next normal working Monday. So it really gets calculated the same with just with difference end of shift time.
24 - (13.5 - 8.5) = 19
or
(24- 13.5) - (8.5 - 0) = 19
For this example 19 will be hard coded into the final formula
3) Determine Number of Weekdays
Based on the description earlier of of NETWORKDAYS.INT and working with the assumption that holidays are stored in the range F2:F2, and using a pattern of "0000011" the number of weekdays the formula will be as follows:
=NETWORKDAYS.INTL(B2,B3,"0000011",F2)
For this example the formula is place in cell F6
4) Determine Number of Saturdays
Similar 3) adjust the pattern to only select Saturdays by using "1111101"
=NETWORKDAYS.INTL(B2,B3,"1111101",F2)
For this example the formula is place in cell F7
5) Determine Number of Sundays
Similar 4) adjust the pattern to only select Saturdays by using "1111110"
=NETWORKDAYS.INTL(B2,B3,"1111110",F2)
For this example the formula is place in cell F8
6) Determine Number of Holidays
To get the number of holidays there is not a direct way of doing it. Instead take the difference between all days counted without holidays being factored in and all days counted with holidays counted in.
=NETWORKDAYS.INTL(B2,B3,"0000000")-NETWORKDAYS.INTL(B2,B3,"0000000",$F$2:F2)
For this example the formula is place in cell F9
Now at this point I would love to say just substitute all of the above into the generic formula, but there are a couple of special cases that need to be taken care of. You may have also noted I have not used the WEEKDAY formula yet.
So in order to count the number of days to which X is going to apply, its really the number of days minus 1. The minus 1 is because you want to cont the intervals between days, not the number of days themselves. This gets a little bit more trickier when the end day is a Saturday because there is still an interval there but Saturday is not counted as a weekday. So the True count for number of weekday intervals is:
=MAX(0,(F6-1+(WEEKDAY(B3,1)=7)))
I originally had the MAX(0, calc) in there to prevent the posibility of the day count being negative. After arriving at this final format it may not be needed and you might get away with the following but its untested:
=F6-1+(WEEKDAY(B3,1)=7)
This same concept needs to be applied to your Saturday count. If you job ends on Saturday you do not need to subtract the non working hours after the last Saturday. You formula will look like:
=MAX(0,(F7-(WEEKDAY(B3,1)=7)))
and again further testing is required to make sure MAX can be removed, but if it can then the formula would look like:
=F7-(WEEKDAY(B3,1)=7)
So now with the understanding how dates and times are stored, determine the time difference between start end end time and subtract all the non working hours.
=(B3-B2)-MAX(0,(F6-1+(WEEKDAY(B3,1)=7)))*TIME(15,0,0)-MAX(0,(F7-(WEEKDAY(B3,1)=7)))*TIME(19,0,0)-F8-F9
Now you will not want to use helper cells, so you can take each of the individual formula from F6 to F9 and wind up with:
=(B3-B2)-MAX(0,(NETWORKDAYS.INTL(B2,B3,"0000011",F2)-1+(WEEKDAY(B3,1)=7)))*TIME(15,0,0)-MAX(0,(NETWORKDAYS.INTL(B2,B3,"1111101",F2)-(WEEKDAY(B3,1)=7)))*TIME(19,0,0)-NETWORKDAYS.INTL(B2,B3,"1111110",F2)-(NETWORKDAYS.INTL(B2,B3,"0000000")-NETWORKDAYS.INTL(B2,B3,"0000000",$F$2:F2))
The formula looks unruly, but is easier to understand when broken down into its parts.
Now the last step is to get the answer to display in minutes. There are two choices.
You can leave it as it is in an excel serial date format and change the formatting of to a custom format of [m]. The [ ] will force it into minutes and prevent spill over to hours. It will also round to the nearest minute.
You can convert the results to minutes by multiplying by 24*60 and the value will be in minutes and decimal of minutes.
Note that:
A11 has Time formatting applied
A12 has General formatting applied
A14 has custom formatting of [m] applied
It should be something like this:
Create a calendar table with the workinghours for each days in the year you have data in
Date | StartTime | End time
1/1/2020 1/1/2020 8:30:00 PM 1/1/2020 5:30:00 PM
...
7/3/2020 7/3/2020 8:30:00 PM 7/6/2020 5:30:00 PM
...
12/31/2020
Then paste this code in a module
Function CalcDays(dStart As Date, dEnd As Date, daysCalendar As Range)
Dim Cell As Range
Dim MinDaysCalendar As Date, MaxDaysCalendar As Date
Dim aWSF As WorksheetFunction
Set aWSF = Application.WorksheetFunction
'check the minimum en the maximum date in the calendar
With aWSF
MinDaysCalendar = .Min(daysCalendar)
MaxDaysCalendar = .Max(daysCalendar)
End With
'if the date you check is not in the calendar, exit the function
If dStart < MinDaysCalendar Or dStart > MaxDaysCalendar Then
MsgBox "Date not in calendar"
Exit Function
End If
If dEnd < MinDaysCalendar Or dEnd > MaxDaysCalendar Then
MsgBox "date not in calendar"
Exit Function
End If
'sum the time of all the dates between the start and the end
'pick min and max in order to start and stop at the right time per day
Dim tempTime As Integer
With daysCalendar
For i = 1 To .Rows.Count
If .Cells(i, 2).Value >= CLng(dStart) And .Cells(i, 3).Value <= CLng(dEnd) Then
daytime = aWSF.Max(.Cells(i, 2).Value, dStart) - aWSF.Min(.Cells(i, 3).Value, dEnd)
End If
tempTime = tempTime + daytime
Next i
End With
'return the total time
CalcDays = tempTime
End Function
You can call the function by typing =calcdays in a cell and then give the startDay, endDay and calendar column as parameters.
There might still be some flaws in this code but I think we can manage those.

Convert sting in datetime like excel format cell does in t-sql

I have END_Date data like 43830.99931. When I paste it in the excel and format the cell to Date. It convert it to 12/31/2019 11:59:00 PM. I want same functionality in T-SQL. How can I achieve same result using T-SQL?
Excel (by default) uses the 1900 date system. This simply means that the date 1 Jan 1900 has a true numeric value of 1, 2 Jan 1900 has a value of 2 etc. These values are called "serial values" in Excel and it is these serial values that allows us to use dates in calculations.
The code to convert:
DECLARE #SerialDate FLOAT;
SET #SerialDate = 43830.99931;
SELECT CAST(#SerialDate - 2 AS DATETIME);
returning back the date time value of:
2019-12-31 23:59:00.383
You may have noticed the -2 in the cast. That's because Excel, due to issues with 29th Feb 1900 and the inclusiveness of the start/end dates. (1900-01-01 is day 1 not day 0, and 1900 was not a leap year but excel calculates it wrongly) we have to subtract 2. Details on that issue can be found here.

Excel 2013 Date time difference

I have a column with date and time for start (IN) and end (OUT) and I want to calculate the difference in days.
Date: IN Time: IN Date: OUT Time: OUT
24/07/2018 12:15:00 26/07/2018 06:11:00
I combine them into date time IN and OUT by using:
E1=TEXT(A2,"dd/mm/yy ")&TEXT(B2,"hh:mm:ss")
F1=TEXT(C2,"dd/mm/yy ")&TEXT(D2,"hh:mm:ss")
But I am unable to find any working code for the difference in number of hours.
TEXT will convert your dates and times into text (strangely enough).
Adding the date and time will return a date/time value so you could use:
=SUM(C2:D2)-SUM(A2:B2) to return 1.74722222
(edit: if you then give the cell a custom number format of d:h:mm it will display 1:17:56 or 1 day, 17 hours, 56 minutes).
(EDIT AGAIN: Sorry, only just seen you want it in hours. Give the cell a custom number format of [hh]:mm. The square brackets tells it to count over 24 hours. This will return 41:56).
or
=DATEDIF(SUM(A2:B2),SUM(C2:D2),"d") to return 2.
or if you just want days from the dates you could just use
=C2-A2 to return 2.
Dates and times in Excel are just numbers - no need to complicate things by turning them to text.
= (F1 - E1) * 24
Rather easly :)

Combining formula using and / or in excel

Case 1
I have a set of data which i need to determine if the cell is in Business Hours or Not.
8 - 18 (08:00 - 18:00) Business Hours (BH)
outside the timeframe is Non Business Hours (NBH)
Given Cell value for example is = "7" (which is NBH)
here is the formula i created =if(AND(C2>=8,C2<=18 ),"BH","NBH")
Case 2
I have a set of data for days in a week, i need to determine if the cell is in Weekdays or Weekends.
I have this formula = =if(OR(I2="Saturday", I2="Sunday"), "NBH", "BH")
note : i used the same variable name NBH - Weekends , BH - Weekdays
What I really need to do is to combine those two cases into 1 formula.
I need to output these scenarios correctly, listing below :
Time is 08:00, date is Saturday/Sunday = Combined formula of case 1 and 2 should output "NBH"
Time is 07:00, date is Monday-Friday = Combined formula of case 1 and 2 should output "NBH"
Time is 12:00, date is Monday-Friday = Combined formula of case 1 and 2 should output "BH"
Formulas can be seen in column BH/NBH WEEKDAYS and BH/NBH Weekends, you can browse attached file thanks much!
Click to access the file
If you want to calculate it directly on the initital values:
=IF(OR(H2="Saturday", H2="Sunday", B2>18, B2<8), "NBH", "BH")
p.s. Alternatively you can combine the already calculated columns, if you intend to keep these columns:
=IF(AND((E2="BH"), (D2="BH")), "BH", "NBH")

Find the Earliest Date in Excel

i want to find the earliest date between the DOB OF FATHER & DOB OF MOTHER in sheet1, by matching the employee code and having the value in earliest date in sheet 2.
Sheet 1
Employee Code DOB OF FATHER DOB OF MOTHER
28883 29/12/1987 28/01/1988
83933 19/11/1988 12/07/1988
55428 21/01/1938 03/10/1938
99999 18/03/1982 11/02/1980
Sheet 2
Employee Code Earliest Date
28883
99999
83933
55428
Sheet1:
A B C
1 Code FatherDOB MotherDOB
2 28883 29/12/1987 28/01/1988
3 83933 19/11/1988 12/07/1988
4 55428 21/01/1938 03/10/1938
5 99999 18/03/1982 11/02/1980
Sheet2:
A B
1 Code EarliestDOB
2 28883 29/12/1987
3 99999 11/02/1980
4 83933 12/07/1988
5 55428 21/01/1938
You can combine two vlookup operations with a min operation:
=MIN(VLOOKUP(A2,Sheet1!$A$2:$C$5,2,FALSE),VLOOKUP(A2,Sheet1!$A$2:$C$5,3,FALSE))
The first vlookup gives you the father's date of birth (using the entire table range but extracting the second column) and the second gives you the mother's date of birth (extracting the third column).
The earliest is then simply the minimum of the two.
If some of the dates may be blank, the easiest solution is probably to set up a D column on sheet 1 to evaluate the earliest date, ignoring blanks. For example D2 would have (split across lines for readability):
=IF(ISBLANK(B2),
B3,
IF(ISBLANK(C2),
B2,
MIN(VLOOKUP(A2,$A$2:$C$5,2,FALSE),
VLOOKUP(A2,$A$2:$C$5,3,FALSE))))
If one of the cells is blank, it uses the other, otherwise it chooses the earliest.
Then you just lookup that new column D in the formula on sheet 2 (example for B2):
=VLOOKUP(A2,Sheet1!$A$2:$D$5,4,FALSE)
I wanted to point out a limitation that has been in Excel forever.
Excel internally doesn't handle dates before 3/1/1900 (March 1, 1900) correctly.
it thinks that 1900 was a leap year
that the day before 3/1/1900 is 2/29/1900
if you enter pass Excel through automation any date between 12/31/1899 and 2/28/1899, it will think it is 1/1/1900 - 2/29/1900
if you attempt to pass it through automation 12/30/1899, it will think it is January 0, 1899.
if you attempt to pass it any date before 12/30/1899, it will throw an error
Various example dates that you can pass to Excel through automation:
Date Excel
-------- ------------------------------
18651001 throws error going into Excel
18991201 throws error going into Excel
18991229 throws error going into Excel
18991230 shows as "12:00:00" in Excel; it refuses to show a date portion
18991231 shows in Excel as 1/1/1900
19000101 shows in Excel as 1/2/1900
19000102 shows in Excel as 1/3/1900
19000103 shows in Excel as 1/4/1900
19000201 shows in Excel as 2/2/1900
19000228 shows in Excel as 2/29/1900
19000229 Feb 29 1900 was not a real date; Excel takes it
19000301 shows in Excel as 3/1/1900
19000601 shows in Excel as 6/1/1900
19001231 shows in Excel as 12/31/1900
19010101 shows in Excel as 1/1/1901
20151128 shows in Excel as 11/28/2015
The VARIANT structure does dictate that a date must be after December 30, 1899 midnight as time zero:
2.2.25 DATE
DATE is a type that specifies date and time information. It is represented as an 8-byte floating-point number.
This type is declared as follows:
typedef double DATE;
The date information is represented by whole-number increments, starting with December 30, 1899 midnight as time zero. The time information is represented by the fraction of a day since the preceding midnight. For example, 6:00 A.M. on January 4, 1900 would be represented by the value 5.25 (5 and 1/4 of a day past December 30, 1899).
tl;dr: If you have any dates in Excel before March 1 1900 (e.g. the birthdate of the oldest woman alive), Excel will not perform the math correctly.
If you wish to display any date before 3/1/1900, it should be presented as text, rather than an actual date.
Anyone attempting to find the minimum date in a range needs to be aware of this limitation in Excel.

Resources