VBSCRIPT - Have Current Time and a Duration and want to calculate start time based on these values - date

Example:
Time = 09:41:46
Duration = 0:00:17 (IE 17 seconds)
Start Time = Time - Duration
Clearly I can't just break this up into hours minutes and seconds and do a basic minus operation given the 60 minute hour and 60 second minute etc.
Can't seem to get my head around how to calculate this and hoping someone has come across this before :).

You can use the DateAdd function.
For example, this will subtract 17 seconds from the specified date/time.
DateAdd("s", -17, "1/1/2013 09:41:46")

Try this
Time = 09:41:46
Duration = 0:00:17
Start_Time = FormatDateTime(Time - Duration, 3)
wscript.echo hour(Start_Time)
wscript.echo minute(Start_Time)
wscript.echo second(Start_Time)
References:
https://www.w3schools.com/asp/asp_ref_vbscript_functions.asp#date

Related

Coldfusion calculate seconds in days, hours, min

I want to convert seconds to days, hours and minutes
Currently, it works just for hours and minutes but not for days. Can you please support me tell me what I did wrong:
<cfscript>
seconds = '87400';
midnight = CreateTime(0,0,0);
time = DateAdd("s", seconds, variables.midnight);
date= xxxxxxxxxxxxxxxxxxxxx???
</cfscript>
<cfoutput>
#DateFormat(variables.date, 'd')# not working
#TimeFormat(variables.time, 'HH:mm')#
</cfoutput>
For the value 87400 the expected result is
1 Days, 0 hours, 16 minutes
If I take 94152 seconds it will be:
1 days, 3 hours, 22 minutes
The only issue i have is to get the correct days ... hours and minutes are diplayed but not the correct days
thank you for all the support
A simple way to calculate the intervals is by taking advantage of the modulus operator:
totalSeconds = 94152;
days = int(totalSeconds / 86400);
hours = totalSeconds / 3600 % 24;
minutes = totalSeconds / 60 % 60;
seconds = totalSeconds % 60;
For 94152 seconds, the results would be:
Interval
Value
DAYS
1
HOURS
2
MINUTES
9
SECONDS
12
TOTALSECONDS
94152
demo trycf.com
I understand from your question that you don't need to get a certain date and time along a timeline, but convert a total amount of seconds in days, hours and minutes. To do that you don't necessary need to use cfml time and date functions like CreateTime() or DateAdd(). You just may need these in order to get a reference point of time or date along a timeline, which doesn't seem to be the case, otherwise you would know the value of your starting date variable. Thus, you can solve this with plain rule of three. There may be simpler methods, so I'm posting an alternative only.
We know that:
60 seconds is equivalent to 1 minute
60 minutes is equivalent to 1 hour
24 hours is equivalent to 1 day
Thus, your calcualtion within cfml could be like so:
<cfscript>
//Constants for calculation
secondsPerDay= 60*60*24;
secondsPerHour= 60*60;
secondsPerMinute= 60;
//Seconds to convert
secondsTotal=87400;
// temp variable
secondsRemain= secondsTotal;
days= int( secondsRemain / secondsPerDay);
secondsRemain= secondsRemain - days * secondsPerDay;
hours= int( secondsRemain / secondsPerHour);
secondsRemain= secondsRemain - hours * secondsPerHour;
minutes= int( secondsRemain / secondsPerMinute);
secondsRemain= secondsRemain - minutes * secondsPerMinute;
writeoutput( "#secondsTotal# seconds are: #days# days, #hours# hours, #minutes# minutes and #secondsRemain# seconds." );
</cfscript>
That outputs:
87400 seconds are: 1 days, 0 hours, 16 minutes and 40 seconds.

Formatting a Countdown Timer When Timer Reaches Negative Numbers

I have an Int, this Int relates to number of minutes. I convert this Int to hours, minutes and seconds using following code and all works great until the timer is below 0.
let hours = Int(timeRemaining) / 3600
let minutes = Int(timeRemaining) / 60 % 60
let seconds = Int(timeRemaining) % 60
timeLeftLabel.text = String(format:"%02i:%02i:%02i", hours, minutes, seconds)
The output from the above code looks something like this and it counts down perfectly:
02:56:07
However, as soon as the time reaches 0 the format starts looking like this:
-2:-56:-7 (this is obviously after minus 2 hours, it doesn't immediately jump to minus 2 hours, I'm just trying to highlight the point)
As you can see, there are numerous issues here, firstly the minus symbol, I simply want there to be one minus symbol at the start. Then the 0's are missing from hours and seconds, it should simply read:
-02:56:07
The only way I can think to try and resolve this is to use: replacingOccurrences(of) but was wondering if there was a better option?
Any advice much appreciated.
A much better solution is to use a DateComponentsFormatter
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.hour, .minute, .second]
formatter.zeroFormattingBehavior = .pad
Example
let timeRemaining: TimeInterval = 2 * 60 * 60 + 56 * 60 + 7
let negative = -1 * timeRemaining
print(formatter.string(from: timeRemaining)!)
print(formatter.string(from: negative)!)
02:56:07
-02:56:07
Since you just want a single negative sign in front of the whole string, start with the absolute value of your interval and do your arithmetic, and then prepend the minus sign if the original was negative.
(But I agree that it is better to use a formatter and no arithmetic.)

Tableau - How to calculate date/time difference and result in full date/time?

I'm trying to calculate a difference between connection time and disconnected time. See image below. But DATEPART formula that I'm using only allows me to use one parameter (hour, minute, second,...)
However, as in the image, I have an ID where disconnection at 3/1/17 2:35:22PM and connection back at 3/2/17 1:59:38 PM
Ideal Response: 23 hours, 24 minutes and 16 seconds
but using the formula:
ZN(LOOKUP(ATTR(DATEPART('minute', [Disconnected At])),-1)-(ATTR(DATEPART('minute', [Connected At]))))
it isn't doing the trick.
Could someone help me to achieve my ideal response? Or similar result that would give me the completeness of date and time?
Thank You
Tableau ScreenShot
Use DATEDIFF by seconds between your two dates. Then create a calc field as follows:
//replace [Seconds] with whatever field has the number of seconds in it
//and use a custom number format of 00:00:00:00 (drop the first 0 to get rid of leading 0's for days)
IIF([Seconds] % 60 == 60,0,[Seconds] % 60)// seconds
+ IIF(INT([Seconds]/60) %60 == 60, 0, INT([Seconds]/60) %60) * 100 //minutes
+ IIF(INT([Seconds]/3600) % 24 == 0, 0, INT([Seconds]/3600) % 24) * 10000 //hours
+ INT([Seconds]/86400) * 1000000 // days
for more information, check out this blog post where I got this from. http://drawingwithnumbers.artisart.org/formatting-time-durations/

AutoHotKey Subtraction and Return Variable

I have started to create a script for WhatsApp web, it is to change a group name and the number of days left till something. However, I want to be able to run the script each day so it takes away one day.
I'm unsure how to do subtraction or how to go about to do this, does AutoHotKey have a return function so I can return the variable at the end.
let's say the number of days is 90
so when I run the script next it will be 89
then the next day after when I run, it will be 87
I'm very new to AutoHotKey and still learning about it but loving it so far.
; FormatTime transforms a YYYYMMDDHH24MISS timestamp into the specified date/time format.
FormatTime, Date, CurrentDate, YYYYMMDD
Expires := 20170611 ; 06/12/2017
; Subtract Date timestamp from Expires timestamp
EnvSub, Expires, CurrentDate, days
; The result is stored in Expires
Msgbox % Expires " days left till ..."
https://autohotkey.com/docs/commands/FormatTime.htm
To run the script each day, create a shortcut of it in your startup folder, or use SetTimer.
Time in days, hours and minutes left till a specific time:
FormatTime, Date, CurrentDateTime, YYYYMMDDHHMI
expires := 201706111537 ; 06/11/2017 15:37
; time left in minutes:
expires_minutes := expires
EnvSub, expires_minutes, CurrentDateTime, minutes
; time left in hours:
expires_hours := expires_minutes
EnvDiv, expires_hours, 60
; time left in days:
expires_days := expires_minutes
EnvDiv, expires_days, (24 * 60)
; rest of the division in hours:
rest_hours := expires_hours - (expires_days * 24)
; rest of the division in minutes:
rest_minutes := expires_minutes - (expires_hours * 60)
Msgbox %expires_days% days, %rest_hours% hours and %rest_minutes% minutes left till ...

Create a countdown timer in iPhone App

I want to use a countdown timer in my app.
I have this date
03-09-2011T20:54:18Z
Now I want to use countdown timer for this date. Can you please suggest if there is any method regarding this. I f I am not clear at any point please let me know. Thanks in advance.
Turn that timestamp into an NSDate* (see here)
Then
NSTimeInterval interval = [timestamp timeIntervalSinceNow];
will always give you the number of seconds between now and the date you care about. Then you can do something like:
#define SECONDS_IN_MINUTE 60
#define SECONDS_IN_HOUR (SECONDS_IN_MINUTE * 60)
#define SECONDS_IN_DAY (SECONDS_IN_HOUR * 24)
NSInteger days, hours, minutes, seconds;
days = interval / SECONDS_IN_DAY;
interval %= SECONDS_IN_DAY;
hours = interval / SECONDS_IN_HOUR;
interval %= SECONDS_IN_HOUR;
minutes = interval / SECONDS_IN_MINUTE;
seconds %= SECONDS_IN_MINUTE;
If you want it to update "live" every second then use performSelector:withObject:afterDelay: with a delay of whatever period you want to update the display (or just 0.0 to do it every run loop)