Adding a specific time to an existing date stamp - vbscript

My boss has asked me to write a function that can determine how much time each employee takes to complete their work in terms of hours spent per assignment. I have created two variables, one for the time work has begun and the other for the time it is completed. The beginning time stamp is to take the exact time the work is started. However, the ending time stamp is to take the day it is completed but I need to add a specific time of day.
For example, I start an assignment on 4/23/2017 4:07:00 PM. I finish the assignment on 4/24/2017. Assignments do not get checked off as completed until about 5:00 PM each day so he wants the time stamp on the finishing time to always be exactly 5:00 PM regardless of the specific time it was marked complete; only the date is of interest here.
I've successfully been able to get the exact timestamps to save but I am having inconsistencies getting my set time to stick to the determined date.
I used:
orderCompleteTimeStamp = CDate(orderCompleteDate + TimeValue("17:00"))
And it seemed to display correctly, however the actual value stored in orderCompleteTimeStamp when used in calculating the time difference is ONLY 5:00, so when it is trying to do 4/23/2017 1:55:26 PM - 4/23/2017 5:00:00 PM, the time difference shows -1028348.9. If I set orderCompleteTimeStamp back to just taking the exact time the calculation works correctly so I can only assume it is something with addition of the specific time.
Any ideas how I can get this to work?

The question isn't very clear, this code works fine for me;
Dim orderStartTimeStamp: orderStartTimeStamp = CDate("23/4/2017 13:55:26")
Dim orderCompleteDate: orderCompleteDate = CDate("23/4/2017")
orderCompleteTimeStamp = CDate(orderCompleteDate + TimeValue("17:00"))
WScript.Echo orderCompleteTimeStamp
WScript.Echo DateDiff("h", orderStartTimeStamp, orderCompleteTimeStamp)
Output:
23/04/2017 17:00:00
4
Assumed you are checking the difference in hours as the question doesn't specify.
If the problem is a date that already has a time component just use DateSerial() to pull out the date components and create a new date variable.
Dim dateFromDB: dateFromDB = "23/4/2017 18:23:02"
Dim orderStartTimeStamp: orderStartTimeStamp = CDate("23/4/2017 13:55:26")
Dim orderCompleteDate: orderCompleteDate = DateSerial(Year(dateFromDB), Month(dateFromDB), Day(dateFromDB))
orderCompleteTimeStamp = CDate(orderCompleteDate + TimeValue("17:00"))
WScript.Echo orderCompleteTimeStamp
WScript.Echo DateDiff("h", orderStartTimeStamp, orderCompleteTimeStamp)
Output:
23/04/2017 17:00:00
4

Related

Google Spreadsheet time between date - hour calculation

I am at a loss, i looked around the internet and stackoverflow but every so called solution is giving either errors or plainly don't work.
I have the following setup.
4 fields (setup in date dd-mm-yyyy, hour hh:mm:ss) seconds are not important.
start date : 7-1-2020
start hour : 23:30:00
end date : 8-1-2020
end hour : 03:50:00
What i want to happen is to calculate the diffrence in 'hours, minutes' between the end and the start date, hour. But when I calculate and change the end date to lets say 09-01-2020 it does not count the extra 24h at all.
Use Text format:
=text(A3-A1+A4-A2,"[H]:MM")
You need to format the time difference as a duration using the custom format
[h]:mm
for hours and minutes
or
[h]
for whole hours.
There are some good notes on how it works in Excel here and as far as I can tell from testing it Google Sheets is the same.
Alternatively, if I read your question as wanting to drop the minutes and seconds from the times before doing the calculation, you could use
=(B3-B1)*24+hour(B4)-hour(B2)
and just format the result as a normal number.
After alot of fiddeling and this post i came to the conclusion that the main issue was not laying within the mathematical but within the format of the cell.
By default all time values in sheets are 24h max.
So the basic formula =start - end
The time format needed should be
more date time format
elapsed hours : elapsed minutes
apply
Now you should see the correct elapsed hours and minutes

Find the difference between 2 dates and check if smaller than a given value

my issue is that I want to be able to get two time stamps and compare if the second (later taken) one is less than 59 minutes away from the first one.
Following this thread Compare two dates with JavaScript
the date object may do the job.
but first thing i am not happy with is that it takes the time from my system.
is it possible to get the time from some public server or something?
cause there always is a chance that the system clock gets manipulated within the time stamps, so that would be too unreliable.
some outside source would be great.
then i am not too sure how to get the difference between 2 times (using 2 date objects).
many issue that may pop up:
time being something like 3:59 and 6:12
so just comparing minutes would give the wrong idea.
so we consider hours too.
biut there the issue with the modulo 24.
day 3 23:59 and day 4 0:33 wouldnt be viewed proper either.
so including days too.
then the modulo 30 thing, even though that on top changes month for month.
so month and year to be included as well.
so we would need the whole date, everything from current year to second (because second would be nice too, for precision)
and comparing them would require tons of if clauses for year, month, etc.
do the date objects have some predfeined date comparision function that actually keeps all these things in mind (havent even mentioned leap years yet, have I)?
time would be very important cause exactly at the 59 minutes mark (+-max 5 seconds wouldnt matter but getting rmeitely close to 60 is forbidden)
a certain function would have to be used that without fail closes a website.
script opens website at mark 0 min, does some stuff rinse and repeat style and closes page at 59 min mark.
checking the time like every few seconds would be smart.
Any good ideas how to implement such a time comparision that doesnt take too more computer power yet is efficient as in new month starting and stuff doesnt mess it up?
You can compare the two Date times, but when creating a date time there is a parameter of DateTime(value) which you can use.
You can use this API to get the current UTC time which returns a example JSON array like this:
{
"$id":"1",
"currentDateTime":"2019-11-09T21:12Z",
"utcOffset":"00:00:00",
"isDayLightSavingsTime":false,
"dayOfTheWeek":"Saturday",
"timeZoneName":"UTC",
"currentFileTime":132178075626292927,
"ordinalDate":"2019-313",
"serviceResponse":null
}
So you can use either the currentFileTime or the currentDateTime return from that API to construct your date object.
Example:
const date1 = new Date('2019-11-09T21:12Z') // time when I started writing this answer
const date2 = new Date('2019-11-09T21:16Z') // time when I finished writing this answer
const diff = new Date(date2-date1)
console.log(diff.toTimeString()) // time it took me to write this
Please keep in mind that due to network speeds, the time API will be a little bit off (by a few milliseconds)

Is there a way to convert the current time / timezone with Apple Script?

I have a basic Apple Script that will return the current time based on the users timezone, however, I would like to expand this to where it will add or subtract hours to adjust to other timezones. For example, If it's 2:29:15 PM CDT, I would like to create a script that will subtract two hours from that to reflect the current time in the Pacific timezone (12:29:15 PM PDT). I would most likely make separate scripts that add or subtract a certain number of hours, so I don't need for this script to handle every timezone.
Here is what I have so far:
set t to (time string of (current date))
Maybe I'm missing something, but if you just want to shift the time of a date by a few hours, you can use a handler like so:
on shiftTimeZone(givenDate, hoursDifference)
return givenDate + (hoursDifference * hours)
end shiftTimeZone
set curd to (current date)
log time string of curd
(*e.g. 6:56:59 PM*)
set newd to shiftTimeZone(curd, -3)
log time string of newd
(*e.g. 3:56:59 PM*)

Calculate two date duration in Elastic 6.7 using painless script

I used below simple expression for getting duration:
doc['endTime'].date.millisOfDay - doc['startTime'].date.millisOfDay
But the problem starts when, endTime crosses the startTime day.
Example: If startTime is 23:50 and endTime for the same is 00:12, we
crossed by midnight, which changes the date as well.
In that way I am getting absolutely wrong duration, except all the scenarios when both time lies with in the same day result is as expected.
Help on how exactly i can make this.
You should simply subtract the absolute milliseconds value since the epoch (instead of milliseconds since the start of the day):
doc['endTime'].date.millis - doc['startTime'].date.millis

How to get latest updated record from sys_user table which is modified at or after certain time stamp in ServiceNow

I want to fetch record from the sys_user table which is updated at or after certain time stamp.
for that I have created rest request as
https:/service-now.com/api/now/v1//table/sys_user?sysparm_query=sys_updated_on>=javascript:gs.dateGenerate('2017-10-30','01:25:00')
I had converted current time which is in IST format into GMT and pass it to dateGenerate() function.
Problem statement -
I don't want to convert the IST to GMT, is there any way by which i can identify ServiceNow instance time zone at runtime and convert given time into that time stamp and get the users.
If i can pass this date and time in UTC format.
Ahoy!
This is a great question, and something that's quite difficult in ServiceNow (dealing with time-zones).
As such, I've written a tool to manage this for you. It's totally free!
The tool is called TimeZoneUtil, and can be found here:
https://snprotips.com/blog/2017/9/12/handling-timezones-in-servicenow-timezoneutil
You simply need to initialize a GlideDateTime object, set its' time-zone to IST, use setDisplayValue() to set its' time based on IST current time, then use .getValue() to get that same time in system time.
This is because getDisplayValue()/setDisplayValue() work based on time-zone, whereas setValue()/getValue() always return the corresponding system time.
EDIT: In order to make this a little more clear, I'll provide some example usage below.
var tz = new TimeZoneUtils(); //initialize with current time
gs.print(tz.getOffsetHours()); //prints out "-7" in my instance, as the system time is in Pacific.
tz.setTimeZone('Asia/Kolkata'); //sets the time-zone to IST/UTC+5.5
gs.print(tz.getOffsetHours()); //prints "5.5".
gs.print(tz.getGDT().getDisplayValue()); //Prints the current time in IST (2017-11-01 20:52:31 at the moment).
gs.print(tz.getGDT().getValue()); //Prints the current time in system time (2017-11-01 15:23:12 at present).
gs.print(new TimeZoneUtils().setTimeZone('Asia/Kolkata').getDisplayValue()); //Single line, also prints current time in IST: 2017-11-01 20:52:31
The first 6 lines there, demonstrate basic usage and explain how it works.
The eighth line however, demonstrates usage on a single line, which you can use inside a query string. For example:
sysparm_query=sys_updated_on>=javascript:new TimeZoneUtils().setTimeZone('Asia/Kolkata').getDisplayValue()
Hope this helps!
Tim Woodruff
Author, Learning ServiceNow & Building Powerful Workflows
Owner/Founder, SN Pro Tips

Resources