Im using visual basic 6.0
Im trying to auto fill my form. text9.text as date of registration and text10.text and expiration date.
I want to add 1 year to text3.text:
Format$(Now, ", mmmm dd, yyyy") the output will be LIKE THIS FOR EXAMPLE: December 15, 2013 (THIS IS THE DATE TODAY) on text2.text
Is there any way I can produce an uotput of: December 15, 2014 into a textbox10.text in the same form as text9.text ?
This the code im using:
Private Sub Form_Load()
Call connect
query = "Select * from Taxi"
rsglob.Open query, connglob
If rsglob.BOF = True Then
Exit Sub
Else
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
Text7.Text = ""
Text8.Text = ""
Text9.Text = Format$(Now, "mmmm dd, yyyy") (out put is: December 15, 2013)
Text10.Text = Format$(Now, ", mmmm dd, yyyy") (Is there any way I make it here as: ( December 15, 2014) ?
End If
Call Gen_ID(Text1)
End Sub
Use the DateAdd function
http://www.vb6.us/tutorials/understanding-vb6s-dateadd-function
DateAdd("yyyy", 1, Now)
Text10.Text = Format$(DateAdd("yyyy", 1, Now), ", mmmm dd, yyyy")
Related
I sure would appreciate some help. I was able to hack away at getting my time formatted as 8:53 AM from a value such as this 7/31/2020 08:53:20 AM with the following.
replace(replace(formatdatetime(formatdatetime(myRS("dateTime"),4)),":00 AM"," AM"),":00 PM"," PM")
But I would think there is a more efficient approach no?
<%#LANGUAGE="VBScript"%>
<%
' The actual LCID.
Response.LCID = 2057
Response.CodePage = 65001
Function AM_PM_Time(TheTime, ZeroPad)
Dim actual_LCID, time_formatted, time_split_1, time_split_2
' Make sure the the date or time being passed is valid.
If NOT isDate(TheTime) Then Exit Function
' Make a note of the actual LCID.
actual_LCID = Response.LCID
' Change the LCID to 1033 so time is formatted as 00:00:00 AM/PM.
' If your LCID is already 1033 you can delete the LCID code.
Response.LCID = 1033
' Format the current date / time.
time_formatted = FormatDateTime(TheTime,3)
' Split the time stamp.
time_split_1 = Split(time_formatted,":")
' Split the AM / PM.
time_split_2 = Split(time_formatted," ")
' Revert the LCID back to its original value.
Response.LCID = actual_LCID
' [Optional]
' Zero-pad the first number of the timestamp, for example: "07:45 PM" rather than "7:45 PM"
If ZeroPad Then
If Len(time_split_1(0)) = 1 Then time_split_1(0) = cStr("0" & time_split_1(0))
End If
' Output the newly formatted time.
AM_PM_Time = time_split_1(0) & ":" & time_split_1(1) & " " & time_split_2(1)
End Function
Response.Write AM_PM_Time(Now(),True) & "<br>"
Response.Write AM_PM_Time("2020-08-17 09:00:00",False) & "<br>"
Response.Write AM_PM_Time("14:55:00",True)
%>
Example output:
07:45 PM9:00 AM02:55 PM
Set speech = CreateObject("sapi.spvoice")
Speech.speak "Here's one way it sets the locale to New Zealand which is 12 hours - it does NOT set time zone"
SetLocale("en-nz")
speech.speak now()
speech.speak "Here's another way" 'if locale is 12 hours it does nothing
Do
If Hour(Now) < 12 then
Var = Hour(Now) & " AM"
else
Var = Hour(Now) - 12 & " PM"
End If
speech.Speak Var & " and " & Minute(Now) & " minutes and " & Second(Now) & " seconds"
wscript.sleep 5
Loop
As you can see it speaks the comments.
when i choose same date from month 10(say dtpckr1 = 02-10-2019 and dtpckr2 = 02-10-2019 ) ..data datagrid does not print anything and shows the msgbox not record found which i code for convinence...but when i choose start date from last moth and end date in this month(say dtpckr1 = 30-09-2019 and dtpckr2 = 02-10-2019 ) it shows all the data from month 09 and nothing from month 10 ... and the strange this is when choose date which is from moth 09 even if it is same(say dtpckr1 = 13-09-2019 and dtpckr2 = 13-09-2019 or 22-09-2019) it works perfectly ..so please try to help me out by refering the following code ..so far i found out that the data which i am getting in datagridview is as per days (dd) not as per whole date...means if i choose the date1 = 31/09/2019 and date2 = 01/10/2019 then it will show the data from date 01 to 31 only from month 09.... I also checked the date format of database and my input,they are same...in databse the date datatype is "date/time" and format is "short date"....if have any other solution then please tell me... i will try... my purpose it to show datewise food orders in datagridview and then calculate the total sale... i am newbie in vb6...so if you can edit my code and repost it ..it will be great...because i want to submit this project by tomorrow..and this is the only which is bothering me... thank you
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub cmdSearch_Click()
Dim date1 As Date
Dim date2 As Date
If IsNull(DTPicker1.Value And DTPicker2.Value) Then
MsgBox "You must select date", vbCritical, "Warning"
Exit Sub
End If
DTPicker1.Value = Format(DTPicker1.Value, "dd-mm-yyyy")
DTPicker2.Value = Format(DTPicker2.Value, "dd-mm-yyyy")
date1 = DTPicker1.Value
date2 = DTPicker2.Value
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\OrderMania\ordermania.mdb;Persist Security Info=False"
rs.CursorLocation = adUseClient
If DTPicker2.Value < DTPicker1.Value Then
MsgBox "End Date Cannot Be Lesser Then Start Date", vbCritical, "Wrong Input"
Exit Sub
Else
Adodc1.RecordSource = "select * from order1 where (date between #" & date1 & "# and #" & DTPicker2.Value & "#)"
Adodc1.Refresh
If Adodc1.Recordset.EOF Then
MsgBox "Please Enter Another Date", vbCritical, "No Record Found"
Else
Adodc1.Caption = Adodc1.RecordSource
End If
End If
con.Close
Call sale
End Sub
Public Sub sale()
Dim i As Integer
Dim Tot, gst, gtot As Double
For i = 0 To Adodc1.Recordset.RecordCount - 1
Tot = Tot + CDbl(DataGrid1.Columns(5).Text)
Adodc1.Recordset.MoveNext
Next i
Text1.Text = Tot
gst = Tot * 0.05
Text2.Text = gst
gtot = Tot + gst
Text3.Text = gtot
End Sub
Try inverting month and day in your between clause :
..."between #" & Format(date1, "mm-dd-yyyy") & "# and #" & Format(date2, "mm-dd-yyyy")) & "#)"
But concatenation of SQL string with variables values is considered bad practice, as #GSerg remind me, since SQL injection of malicious code could occurs. You should work with parameters. If you want to study this, here is a start point : https://learn.microsoft.com/fr-fr/office/client-developer/access/desktop-database-reference/createparameter-method-ado
Trying to Achieve
I fixed a date on my code say 31-01-2019. Then everyday I will execute my code but only on 28-02-2019/29-02-2020, 31-03-2019, 30-04-2019... I wish to execute the code. It is something like monthly generation. In addition, if the fixed date is 30-01-2019, I wish to execute the code on 28-02-2019/29-02-2020, 30-03-2019, 30-04-2019...
For example
What I have done
I have followed the question VBScript DateDiff month, and have tried out the following code but it is not working.
If I were to have a date say 31-Jan-2010 by DateAdd
endFeb = DateAdd("m",1,"31-Jan-10")
endMar = DateAdd("m",1,endFeb)
endApr = DateAdd("m",1,endMar)
The result
endFeb: 28/02/2010
endMar: 28/03/2010
endApr: 28/04/2010
What I want is
endFeb: 28/02/2010
endMar: 31/03/2010
endApr: 30/04/2010
Code
sFixedDate = "2019-01-31" '==== Fixed
sProcessDate = "2019-02-28" '==== Changes daily
d1 = CDate(sFixedDate)
d2 = CDate(sProcessDate)
diff = DateDiff("m", d1, d2)
If request("btnProcess") <> "" Then
If diff Mod 1 = 0 Then '=== Not as simple as I thought
'=== Trying to do monthly GENERATION.
'===Excecute the CODE
End If
End If
Basically, you want to run something on the last day of each month. Meaning that the day after would be a different month, so you could do something like this for calculating the last day of the next month:
today = Date
tomorrow = today + 1
If request("btnProcess") <> "" Then
If Month(today) <> Month(tomorrow) Then
endNextMonth = DateAdd("m", 1, tomorrow) - 1
End If
End If
To get the last day for any given month adjust the number of months to add to tomorrow's date.
The above assumes that you're doing the calculation on the last day of a month. If you want to calculate the last day of any given month on any day of a month please see Ekkehard Horner's answer.
Use DateSerial:
For m = 1 To 13
d1 = DateSerial(2019, m, 1) ' First day of month is easy
d2 = DateAdd("d", d1, -1) ' Last day of previous month is just 1 day before
WScript.Echo m, d1, d2
Next
cscript lom.vbs
1 01.01.2019 31.12.2018
2 01.02.2019 31.01.2019
3 01.03.2019 28.02.2019
4 01.04.2019 31.03.2019
5 01.05.2019 30.04.2019
6 01.06.2019 31.05.2019
7 01.07.2019 30.06.2019
8 01.08.2019 31.07.2019
9 01.09.2019 31.08.2019
10 01.10.2019 30.09.2019
11 01.11.2019 31.10.2019
12 01.12.2019 30.11.2019
13 01.01.2020 31.12.2019
It seems like for a given start date, you want to calculate x months into the future what that new date is, and if the start date as a day that is greater than the future month, to give the last day of the month instead.
Function CalculateFutureDate(startDate, monthsInFuture)
' Assumes startDate is in the past
Dim dtRepeatDate
Dim dtNewDate
If (IsDate(startDate)) Then
dtRepeatDate = CDate(startDate)
' months between now and Start Date
Dim intMonthsToAdd
Dim dtCurrentDate
dtCurrentDate = Now()
intMonthsToAdd = DateDiff("m", startDate, dtCurrentDate)
If intMonthsToAdd > 0 And Day(startDate) < Day(dtCurrentDate) Then
intMonthsToAdd = intMonthsToAdd - 1
End If
' Add the future months to the month offset
intMonthsToAdd = intMonthsToAdd + monthsInFuture
' Now calculate future date
dtNewDate = DateAdd("m", intMonthsToAdd, dtRepeatDate)
CalculateFutureDate = dtNewDate
End If
End Function
And then you can do something like:
CalculateFutureDate(CDate("2019-01-31"), intFutureMonths)
This will output:
?CalculateFutureDate(CDate("2019-01-31"), 1)
2/28/2019
?CalculateFutureDate(CDate("2019-01-31"), 2)
3/31/2019
?CalculateFutureDate(CDate("2019-01-31"), 3)
4/30/2019
dtLoan = CDate("2019-01-30")
dtProcess = CDate ("2020-02-28")
'dtLoan = CDate("2019-01-31")
'dtProcess = CDate ("2020-02-29")
'dtLoan = CDate("2019-02-28")
'dtProcess = CDate ("2020-02-29")
if LastDateOfMonth(dtLoan) = dtLoan AND dtProcess = LastDateOfMonth(dtProcess) then
response.write " this mean that the Loan date is end of the month, say 31 Jan, 28, 29 of Feb, 31 Feb "
response.write " and Process Date is also end of the month " & "<br>"
response.write " **** End of the month Loan Date : " & dtLoan & "<br>"
response.write " **** End of the month Process Date : " & dtProcess & "<br>"
elseif LastDateOfMonth(dtLoan) <> dtLoan AND dtProcess <> LastDateOfMonth(dtProcess) then
daysFromEndOfLoanMth = DateDiff("d",LastDateOfMonth(dtLoan),dtLoan)
response.write " How many days from end of Loan month: " & daysFromEndOfLoanMth & "<br>"
daysFromEndOfProcessMth = DateAdd("d",daysFromEndOfLoanMth,LastDateOfMonth(dtProcess))
response.write " From end of the month Add " & daysFromEndOfLoanMth & " Days = " & daysFromEndOfProcessMth & "<br>"
response.write " The date of process : " & dtProcess & "<br>"
dtShouldProcess = day(dtLoan) & "/" & Month(dtProcess) & "/" & Year(dtProcess)
if isDate(dtShouldProcess) then
dtShouldProcess=CDate(dtShouldProcess)
else
dtShouldProcess=daysFromEndOfProcessMth
end if
response.write " ** The date of should Process : ** " & dtShouldProcess & "<br>"
if dtProcess = dtShouldProcess then
'if dtProcess = daysFromEndOfProcessMth then
response.write " **** Loan Date : " & dtLoan & "<br>"
response.write " **** Process Date : " & dtProcess & "<br>"
end if
'daysFromEndOfProcessMth = DateDiff("d",LastDateOfMonth(dtProcess1),dtProcess1)
'response.write " How many days from Process Date end of the month: " & daysFromEndOfProcessMth & "<br>"
end if
I have to delete data based on its filename. This is what the files look like:
Nostro_BO_FCC_130317.csv [130317 is a created date]
Nostro_BO_FCC_120317.csv
Nostro_BO_FCC_110317.csv
Nostro_BO_FCC_100317.csv
Nostro_BO_FCC_090317.csv
and this is where the data located: D:\BDI\CTS\Data\Nostro\BO FCC\
I have developed VBScript to delete the file but it does not work at all. All I want is to delete the file which below 2 days since current date (13/03/2017).
This is my VBScript:
Dim infolder
Dim ad, intcount, i, str, Postdate, Uploaddate, fileExists, ExpireDate
Dim sql_query, rs, rsU
Dim ObjFSO, objFile, OFile, OfPath, osf, MM, DD
Set ad = CreateObject("ADODB.Connection")
ad.Provider = "sqloledb"
If Len(Month(varDate)) = 1 then
MM = "0" & Month(varDate)
Else
MM = Month(varDate)
End If
If Len(Day(varDate)) = 1 then
DD = "0" & Day(varDate)
Else
DD = Day(varDate)
End If
PostDate = Year(varDate) & MM & DD
Uploaddate = DD & MM & Right(Year(varDate), 2)
ExpireDate = CDate(DD) < Date - 1 & MM & Right(Year(varDate), 2)
ad.CursorLocation = 3
ad.Open propstr
Set osf = CreateObject("Scripting.FileSystemObject")
OfPath = "D:\BDI\CTS\Data\Nostro\BO FCC\"
'this below my logic steven
Set infolder = osf.GetFolder(OfPath)
Set OFile = Nothing
fileExists = True
fullfilename = "Nostro_BO_FCC_"& Uploaddate &".csv"
'create file if not exits and delete if exits then create again
If Not osf.FileExists(OFPath & fullfilename) Then
Set OFile = osf.CreateTextFile(OFPath & fullfilename, True)
Set OFile = Nothing
End If
For Each file In infolder.Files
If DateDiff("d", file.DateCreated, Date) < Date -2 Then
' oFSO.DeleteFile(oFile)
'If osf.FileExists(OfPath & "Nostro_BO_FCC_" & ExpireDate & ".csv") Then
'osf.DeleteFile OfPath & "Nostro_BO_FCC_" & ExpireDate & ".csv"
file.Delete(True)
End If
Next
CDate(DD) < Date -1 & MM & Right(Year(varDate),2) isn't going to do what you apparently expect it to do. I already told you that when answering your previous question where you used a similar construct.
If you want to compare date strings with a < or > operator, the strings must be in a format where string order and date order are identical. That is not the case for your DDMMYY format. Because of that you basically have two options:
Since you have a small number of valid dates you could build reference filenames:
Function dd(s) : dd = Right("00" & s, 2) : End Function
d1 = Date
d2 = d1 - 1
d3 = d1 - 2
fn1 = "Nostro_BO_FCC_"& dd(Day(d1)) & dd(Month(d1)) && Right(Year(d1), 2) &".csv"
fn2 = "Nostro_BO_FCC_"& dd(Day(d2)) & dd(Month(d2)) && Right(Year(d2), 2) &".csv"
fn3 = "Nostro_BO_FCC_"& dd(Day(d3)) & dd(Month(d3)) && Right(Year(d3), 2) &".csv"
and delete all files whose name isn't among them:
For Each f In infolder.Files
If f.Name <> fn1 And f.Name <> fn2 And f.Name <> fn3 Then
f.Delete
End If
Next
A more generic approach is to parse the date from each filename:
a = Split(osf.GetBaseName(f), "_")
ds = a(UBound(a))
d = DateSerial(Mid(ds, 5, 2), Mid(ds, 3, 2), Mid(ds, 1, 2))
and delete all files with a date below your reference date:
refDate = Date - 2
For Each f In infolder.Files
...
If d < refDate Then
f.Delete
End If
Next
Edit: If you want to compare the files' creation date against a reference date you can do that like this:
refDate = Date - 2
For Each f In infolder.Files
If f.DateCreated < refDate Then
f.Delete
End If
Next
This question already has an answer here:
Format current date and time in VBScript
(1 answer)
Closed 4 years ago.
My issue is when I retrieve date back it gives me in this format:
lastseenstatus=rsprefobj("lastseentstamp")
19-07-2014 15:31:32
I want it in 7/19/2014 3:31:32 PM format with AM/PM intact.
Please help..
First and foremost you need to determine the data type of rsprefobj("lastseentstamp"):
MsgBox TypeName(rsprefobj("lastseentstamp"))
If it's a string, you need to convert it to a datetime value first:
lastseenstatus = CDate(rsprefobj("lastseentstamp"))
If you want the date formatted according to the system's regional settings, use the FormatDateTime() function as #John suggested:
MsgBox FormatDateTime(lastseenstatus)
If you need a distinct date format regardless of the system's regional settings you have to either build the formatted string yourself:
Function LPad(v) : LPad = Right("00" & v, 2) : End Function
Function FormatDate(d)
formattedDate = Month(d) & "/" & LPad(Day(d)) & "/" & Year(d) & " " & _
((Hour(d) + 23) Mod 12 + 1) & ":" & LPad(Minute(d)) & ":" & _
LPad(Second(d))
If Hour(d) < 12 Then
formattedDate = formattedDate & " AM"
Else
formattedDate = formattedDate & " PM"
End If
FormatDate = formattedDate
End Function
MsgBox FormatDate(lastseenstatus)
or use the .Net StringBuilder class:
Set sb = CreateObject("System.Text.StringBuilder")
sb.AppendFormat "{0:M\/dd\/yyyy h:mm:ss tt}", lastseenstatus
MsgBox sb.ToString()
In my tests I wasn't able to get the tt format specifier to work, though, so you may have to resort to something like this:
Set sb = CreateObject("System.Text.StringBuilder")
If Hour(lastseenstatus) < 12 Then
am_pm = "AM"
Else
am_pm = "PM"
End If
sb.AppendFormat_5 Nothing, "{0:M\/dd\/yyyy h:mm:ss} {1}", _
Array(lastseenstatus, am_pm)
MsgBox sb.ToString()
I'm assuming you are using VBScript and not VB.NET like you have tagged.
Use FormatDateTime(lastseenstatus).
That should give you the format "2/16/2010 1:45:00 PM".