How to get month & year from the date
Date = 01/02/2012 (dd/mm/yyyy)
From the above date, i want to get 02/2012 (mm/yyyy)
How to write a code in vb6
Need vb6 code help
You could use the Month() and Year() functions which would take a Date parameter.
I.e. Month(Now) would return the Month and Year(Now) would return the Year.
Dim yourDate As Date
yourDate = #01/02/2012#
Dim yourMonth As Integer
Dim yourYear As Integer
yourMonth = Month(yourDate)
yourYear = Year(yourDate)
You can use Format$:
Dim formattedDate as String
Dim myDate as Date
myDate = #1/2/2012#
formattedDate = Format$(myDate, "mm/yyyy")
Related
How can I convert the week number to the starting date of that week? Example: week 41 to October 9, 2016? The date must also depend on the year. Example: For the year 2017, week 41 must equal to October 8.
I've already searched and found this (How to get starting date in a Week based on week number using vb.net?) but it is for vb.net. I am not sure if this is applicable in vb6. If this does not work in vb6, how can I do it?
Thanks :)
Try this:
Private Sub Form_Load()
Dim d As Date
d = GetWeekStartDate(41, 2016)
MsgBox d
d = GetWeekStartDate(41, 2017)
MsgBox d
End Sub
Private Function GetWeekStartDate(weekNumber As Integer, year As Integer) As Date
Dim startDate As Date
Dim day As Integer
startDate = DateSerial(year, 1, 1)
day = Weekday(startDate, vbSunday)
startDate = DateAdd("d", DaysToAdd(day), startDate)
GetWeekStartDate = DateAdd("ww", weekNumber - 1, startDate)
End Function
Private Function DaysToAdd(day As Integer) As Integer
DaysToAdd = 0
If day > 1 Then DaysToAdd = 7 - day + 1
End Function
You can simplify this if you don't need all of the options, but it looks about right:
Option Explicit
Private Function WeekDate( _
ByVal Year As Long, _
ByVal Week As Long, _
Optional ByVal FirstWholeWeekIs1 As Boolean, _
Optional ByVal FirstDayOfWeek As VbDayOfWeek = vbUseSystemDayOfWeek) As Date
Dim YearDate As Date
Dim WeekdayOffset As Long
Dim WeekOffset As Long
YearDate = DateSerial(Year, 1, 1)
WeekdayOffset = Weekday(YearDate, FirstDayOfWeek) - 1
If WeekdayOffset <> 0 Then
YearDate = DateAdd("d", -WeekdayOffset, YearDate)
WeekOffset = IIf(FirstWholeWeekIs1, 0, 1)
Else
WeekOffset = 1
End If
WeekDate = DateAdd("ww", Week - WeekOffset, YearDate)
End Function
Private Sub Form_Load()
Dim Year As Long
Dim Week As Long
AutoRedraw = True
Year = 2015
Week = 1
Print Year, Week, WeekDate(Year, Week)
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=True)
Week = 2
Print Year, Week, WeekDate(Year, Week)
Print Year, Week, WeekDate(Year, Week, FirstDayOfWeek:=vbMonday)
Year = 2016
Week = 41
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=True)
Year = 2017
Week = 1
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=False)
Print Year, Week, WeekDate(Year, Week, FirstWholeWeekIs1:=True)
End Sub
If not quite right I'm sure you could make adjustments.
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".
I want to calculate time difference in vb6. i had been done it in vb.net. but i don't know how to convert in vb6.Because the datetime type will not be available in vb6.
i have pasted my vb.net code below. how to do the same in vb6?
Public Sub timecal()
Dim dFrom As DateTime
Dim dTo As DateTime
Dim tempstarttime As DateTime
Dim idletime As String = "00:05:00"
Dim Needtosub As String = "00:01:00"
Dim timeDiff As String
If DateTime.TryParse(start_time, dFrom) AndAlso DateTime.TryParse(end_time, dTo) Then
Dim TS As TimeSpan = dTo - dFrom
Dim hour As Integer = TS.Hours
Dim mins As Integer = TS.Minutes
Dim secs As Integer = TS.Seconds
timeDiff = ((hour.ToString("00") + ":") + mins.ToString("00") + ":") + secs.ToString("00")
sscheck1 = False
If timeDiff >= idletime Then
tempstarttime = System.DateTime.Parse(end_time)
tempstarttime = tempstarttime.AddMinutes(-1)
start_time = Strings.Format(tempstarttime, "yyyy-MM-dd HH:mm:ss")
sscheck1 = True
End If
End If
End Sub
You can calculate the time difference between two Date values (which will include the time) using the DateDiff function
For difference in minutes:
Dim lMinutes as Long
lMinutes = DateDiff("n", dFrom, dTo)
For difference in seconds:
Dim lSeconds as Long
lSeconds = DateDiff("s", dFrom, dTo)
For difference in hours:
Dim lHours as Long
lHours = DateDiff("h", dFrom, dTo)
For the purposes of getting your string time diff in hours:minutes:seconds, I would do:
Dim lSeconds as Long, lMinutes as Long
lSeconds = DateDiff("s", dFrom, dTo)
lMinutes = Fix(lSeconds / 60) '// Gets the whole number (not rounded)
lSeconds = ((lSeconds / 60) - lMinutes) * 60 '// get the remaining seconds
sTimeDiff = "00:" & Format$(lMinutes, "00") & ":" & Format$(lSeconds, "00")
Note: If lMinutes is greater than 60, you'll need to do similar math to pull out the hours portion before pulling the minutes and seconds. This code assumes your timespan is less than an hour based on your example.
use vb DateDiff Function i.e
Dim datTim1 As Date = #1/4/2001#
Dim datTim2 As Date = #1/9/2001#
' Assume Sunday is specified as first day of the week.
Dim wD As Long = DateDiff(DateInterval.Weekday, datTim1, datTim2)
Dim wY As Long = DateDiff(DateInterval.WeekOfYear, datTim1, datTim2)
or can be done with another general way
Dim dtStart As Long
Dim dtEnd As Long
Dim result As Long
Dim i As Integer
dtStart = GetTickCount
dtEnd = GetTickCount
result = dtEnd – dtStart // miliseconds
OutAddIn.objApptItem.Start = CDbl(result) // miliseconds to date
I am taking the dates through datepicker controls, but not achieving any success, anyone have any suggestions.
I tryed:
Dim fromDate As New DateTime(startdate_picker_search_supplierinvoice.Text)
Dim toDate As New DateTime(enddate_picker_search_supplierinvoice.Text)
query &= "supplier_invoice_date >= '" & fromDate & "' AND supplier_invoice_date <= '" & toDate & "'"
invoicesresults_datagrid_search_supplierinvoice.DataSource = SelectDataTable(dt, query)
Error: Conversion from string "Tuesday, July 17, 2012" to type 'Long' is not valid.
Don't use the .Text property which is returning the full written out date. Instead use:
Dim fromDate As DateTime = startdate_picker_search_supplierinvoice.Value
Dim toDate As DateTime = enddate_picker_search_supplierinvoice.Value
and then the `.ToString' extension.
query &= "supplier_invoice_date >= '" & fromDate.ToShortDateString & "' AND supplier_invoice_date <= '" & toDate.ToShortDateString & "'"
invoicesresults_datagrid_search_supplierinvoice.DataSource = SelectDataTable(dt, query)
The problem is that you are declaring a DateTime object, and the constructor you are using is expecting (ticks as Long). You should be using
Dim toDate As DateTime
toDate = enddate_picker_search_supplierinvoice.Value
Note that .Value returns the date, rather than a String.
label1 displays the last transaction date/time which I get from a database through a query. label2 is the system date/time. I have a timer that executes a command button after which I want to check if the date/time in label1 is smaller than 5 minutes. If so then I want to show a massage.
But I don’t know why my code is failing to perform this function.
Any help will be much appreciated.
Private Sub Command1_Click()
Dim date1 As Date
Dim date2 As Date
date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
date2 = Format(label1, "yyyy/mm/dd hh:mm:ss")
If DateDiff("n", date1, date2) < 2 Then
MsgBox ("Not Vending")
End If
End Sub
I've also tried:
Private Sub Command1_Click()
Dim date1 As Date
Dim label1 As Date
date1 = Format(Now, "yyyy/mm/dd hh:mm:ss")
date2 = label1
If DateDiff("m", Now, date1) > DateDiff("m", Now, label1) Then
MsgBox ("Not Vending")
End If
End Sub
As well as:
Private Sub Command1_Click()
If DateDiff("n", Now, label1) > 5 Then
MsgBox ("Not Vending")
End If
End Sub
If the date pulled from the DB is earlier than Now, DateDiff will always return a negative number if you pass Now as the second parameter. It looks like you're checking for the passage of time, so I'll assume the dates in DB will always be before Now. You need to switch the order of Now and the date to which it is being compared (DateDiff("n", date1, Now) instead of DateDiff("n", Now, date1).
Private Sub Command1_Click()
Dim date1 As Date
date1 = CDate(Label1.Caption)
If DateDiff("n", date1, Now) < 5 Then
MsgBox ("Not Vending")
End If
End Sub
An other way without using the DateDiff-function would be direct calculation with the Date-variables - since under the hood these are actually Doubles.
Private Sub Command1_Click()
Dim date1 As Date
date1 = CDate(Label1.Caption)
'#12:05:00 AM# is representing a value of 5 Minutes in the Date-datatype
If (date1 - Now) < #12:05:00 AM# Then
MsgBox ("Not Vending")
End If
End Sub
If you want to check the time difference you can also use
Private Sub Command1_Click()
Dim date1 As Date
date1 = CDate(Label1.Caption)
'#12:05:00 AM# is representing a value of 5 Minutes in the Date-datatype
If Abs(date1 - Now) < #12:05:00 AM# Then
MsgBox ("Not Vending")
End If
End Sub
Where Abs(date1 - Now) returns the time difference as Date-value
Btw. date1 = Format(Now, "yyyy/mm/dd hh:mm:ss") doesn't make sence. Since Now returns a Date-value, Format converts that Date-value into a String and assigning it to date1 converts that String back to a Date-value ùsing the local system settings for date formats - which means that the program will behave different on different systems.