Command button enable/disable based on textbox value - date

I am trying to enable/disable a command button based on the value of a textbox. Ex. "08-09-2015 15:06:24", taken from a table column field
It seems that it will either enable OR disable it, depending on < or >.
I want it to find out if txt.Value is MORE than 15 hours ago, then it should activate the button. If not, leave it "false"
The textbox and command button are on the same form.
This is what I have so far, and apparently not working.
Public Sub Kommandoknap184_Click()
If Me.txtOpdTid.Value < DateAdd("h", -15, Date) Then
Kommandoknap35.Enabled = False
Else
Kommandoknap35.Enabled = True
End If
End Sub

Date() gives you the current date with midnight as the time. Now() gives you the current date and time. So I think you want Now() instead of Date().
Public Sub Kommandoknap184_Click()
If Me.txtOpdTid.Value < DateAdd("h", -15, Now) Then
' Value is MORE than 15 hours ago, then it should activate the button
Me.Kommandoknap35.Enabled = True
Else
Me.Kommandoknap35.Enabled = False
End If
End Sub

It looks like there are two problems with your code.
In the DateAdd function you want to use Now() instead of Date(). Now() will include the current time along with the date. This is important since you are comparing the number of hours and not the number of days.
The enable/disable logic is backwards (enabling the button when it should be disabled and vice-versa).
Below is a simplified version with the corrections:
Public Sub Kommandoknap184_Click()
Dim isMoreThan15HoursAgo As Boolean
isMoreThan15HoursAgo = Me.txtOpdTid < DateAdd("h", -15, Now)
Me.Kommandoknap35.Enabled = isMoreThan15HoursAgo
End Sub
You might also consider having this code run in the After Update event of the text box instead of when clicking a separate button. This would make the enable/disable of the button more seamless.

Related

Pine scripting: how to find the price X days ago

In Pine Script, how do I find the price based on a certain number of days ago? I've tried something like this...
// Find the price 90 days ago
target = time - 90 * 60 * 60 * 24 * 1000
valuewhen(time < target, close, 1)
...however time < target never seems to return true – presumably because the current bar's time cannot also be in the past at the same time. Perhaps valuewhen() wasn't designed to be used with dynamic values that change on every bar?
Do I need to use a loop instead, and scan through every past bar until I find the date I'm looking for?
Perhaps there's a better way, but the workaround I'm using currently using is a function with a for loop, scanning backwards until the appropriate date is found. Here is my function:
priceXDaysAgo(numDays) =>
targetTimestamp = time - numDays*60*60*24*1000
// Declare a result variable with a "void" value
float result = if false
1
// We'll scan backwards through the preceding bars to find the first bar
// earlier than X days ago (it might be a little greater than X days if
// there was a break in trading: weekend, public holiday, etc.)
for i = 1 to 1000
if time[i] < targetTimestamp
result := close[i]
break
result
You can then call the function anywhere in your script:
priceXDaysAgo(90)

Convert unix time to month number?

Using os.time how can I get how many months have passed since the unix epoch (Unix Timestamp)
I just need it for a month ID, so any kind of number would be fine, as long as it changes every month, and it can be reversed to get the actual month.
local function GetMonth(seconds)
local dayduration,year = 3600*24
local days={31,0,31,30,31,30,31,31,30,31,30,31}
for i=1970,10000 do -- For some reason too lazy to use while
local yeardays = i%4 == 0 and i%100 ~= 0 and 366 or 365
local yearduration = dayduration * yeardays
if yearduration < seconds then
seconds = seconds - yearduration
else
year = i break
end
end
days[2]=(year%4==0) and 29 or 28
seconds = seconds%(365*24*3600)
for i=1,12 do
if seconds>days[i]*dayduration then
seconds=seconds-days[i]*dayduration
else
return --i + year*12 <-- If you want a unique ID
end
end
end
Currently, it'll give the number 2, since it's February. If you uncomment the code at the end for the unique ID, you'll get 554 instead, meaning we're currently at the 554th month since the epoch.
As Jean-Baptiste Yunès said in his answer's comments, I'm not sure if your sentence:
NOTE: This is for Lua, but I'm unable to use os.date
meant you have no os.date, or that you don't know how to use it. You have an answer for both cases, you can use the one you need.
This may do the trick:
print (os.date("*t",os.time())["month"])
os.time() gives you the current date as a number. os.date("*t",...) converts it into a table in which the month equals to the number of the month corresponding to the date.

Comparing date modified with specific date (VBS)

I'm trying to compare a file's modified date with a specific date.
What I have is this:
If FormatDateTime(objFile.DateLastModified,vbShortDate) = specificDate Then
'Do something
End if
I've tried using IsDate and a variable with a value of #11/9/2015# but always returns false. I can't figure out how to set the variable "specificDate" to 11/9/2015.
If you are comparing the date only (but not the time), then you have to cut off the fractional part of the last modified value, since integer part represents days, and fractional part - hours, minutes and seconds. After any changes convert the values back to Date type with CDate(), as well as date containing string before the comparison.
Sub Test()
dtSpecificDate = CDate("11/9/2015")
With CreateObject("Scripting.FileSystemObject").GetFile("C:\Test\tmp.txt")
dtLastModified = CDate(Int(.DateLastModified))
End With
If dtLastModified = dtSpecificDate Then
' Do something
End If
End Sub

AppleScript (or swift) add hours to time

I'm trying to add 8Hours to a date (from the clipboard)
set date1 to the clipboard
set newdate to date1 + (8 * hours)
display dialog "Purchases were downloaded at " & newdate buttons {"OK"} default button 1
But this is not working as expected, I'm having the error
Can’t make "06/22/2015 08:15:27 " into type number.
You need to pick the hours as an individual variable, like shown below:
set currentDate to current date
set newHour to ((hours of currentDate) + 8)
You can also use this for days, minutes and seconds.
This will work. You can then use the variables to construct a new date to be used in the display dialog.
PS. Don't forget to change the day if the newHour variable is bigger than 24 hours.
EDIT
Setting a date to the clipboard can be done like this:
set currentDate to current date
set the clipboard to currentDate as Unicode text
Getting the current clipboard and adding it to a variable goes like this:
set currentDate to get the clipboard
display dialog currentDate
I hope this helps!
In Swift, you can call dateByAddingTimeInterval on an NSDate object.
The time interval is measured in seconds.
yourDate.dateByAddingTimeInterval(8 * 60 * 60)
If you wanted to add another method to add 8 hours directly, you could define an extension:
extension NSDate {
func addEightHours() -> NSDate {
return self.dateByAddingTimeInterval(8 * 60 * 60)
}
}

VB Scripting - comparing time values, one coming from a text file?

I'm attempting to pull some information out of text file that is updated after I query a piece of equipment. The text file contains lines such as shown here (abbreviated):
05-Nov-13 11:11:54.3496 ( -1 7020 10244) scpeng.exe:Automation Server...
05-Nov-13 14:10:54.3496 ( -1 7020 10244) scpeng.exe:Automation Server...
05-Nov-13 14:10:54.3496 ( -1 7020 10244) scpeng.exe:Automation Server...
05-Nov-13 14:10:56.3496 ( -1 7020 10244) scpeng.exe:CServer.cpp,....
The text file can contain up to several weeks of information. I have a subroutine that will run a few seconds after I query the equipment which should allow for the reply and the applicable line to be present in the text file. In the routine, I am trying to scroll through the lines examining the date to arrive at the date of the subroutine call followed by the time (or a time ~10 seconds prior the the current time) to arrive at the lines pertinent to where the information could be found.
do
msg = msgstream.ReadLine
logdate = mide(msg,1,9)
logday = Cdate(logdate)
loop while logday < date
do
msg = msgstream.Readline
logtime = mid(msg,12,8)
'logtime = CDate(logtime) This mod is not working
loop while logtime < time
The date loop appears to work however the time is giving me problems. It does not error out but I can't get it to run beyond one line of text. Can anyone suggest a fix or better option? I have read that the built-in Date function can include the time but I do not believe this version I'm using does. Also, the text file contains times in a 24 hour format where I believe the time function returns values in a 12 hr format ie "12:43:27 PM ST".
You're making this way too complicated. Simply parse the whole date string into a datetime value:
refdate = Now
Do
msg = msgstream.ReadLine
logdate = CDate(Mid(msg, 1, 19))
Loop While logdate < refdate
You can extract date and time portions from the value later, e.g. like this:
WScript.Echo DateValue(logdate)
WScript.Echo TimeValue(logdate)
Also, Time returns the current (unformatted) system time. Whether it's displayed in 12 hour or 24 hour format depends on your system's region settings. However, you can always get the hour (0-23) by using the Hour function.
Parse each line with a regex to get the correct date and time part. I prefer a regexp above string manipulation functions because you can separate format and code.
Reassemble the date from the two parts and see if the date is smaller than yesterday at this time.
Option Explicit
dim strTest, re, matches, myDatePart, myTimePart, logDate
' teststring
strTest = "08-Nov-13 14:10:56.3496 ( -1 7020 10244) scpeng.exe:CServer.cpp,...."
Set re = new regexp
' This pattern extracts two part, the date as (dd-www-dd) and the time as (hh:mm:ss)
re.pattern = "(\d{2}-\w{3}-\d{2}) +(\d{2}:\d{2}:\d{2})"
Set matches = re.Execute(strTest)
' Get the first and second submatch to define the date and time
myDatePart = matches(0).submatches(0)
myTimePart = matches(0).submatches(1)
' datevalue and timevalue automatically tranforms to Date type
logDate = datevalue(myDatePart) + timevalue(myTimePart)
' See if the date is smaller than yesterday exactly this time
msgbox (logDate < (DateAdd("d", -1, now))) ' Returns True, because 08 Nov is earlier than yesterday.