In the code below, I'm using the current date and time, with the help of which I am generating a file name. My problem is that it's giving me an output without leading zeros:
Dim strDateTime As String = DateTime.Now.Day.ToString() & "" & _
DateTime.Now.Month.ToString() & "" & DateTime.Now.Year.ToString() & "" & _
DateTime.Now.Hour.ToString() & "" & DateTime.Now.Minute.ToString() & "" & _
DateTime.Now.Second.ToString() & DateTime.Now.Millisecond.ToString()
For example, my query is giving output as below currently:
Assume time is 1:5:30 :: hh:mm:ss
Required output is: 01:05:30
How can I achieve this?
Try this,
DateTime.Now.Hour.ToString("00") & ":" & DateTime.Now.Minute.ToString("00") & ":" & DateTime.Now.Second.ToString("00")
EDIT :
As suggested by 'mdb' in the answers, using Custom Date and Time Format Strings would be more efficient and cleaner
DateTime.Now.ToString("hh:mm:ss tt") '12 Hour format with AM/PM designator, Eg :- 09:01:01 PM
DateTime.Now.ToString("HH:mm:ss") '24 Hour format Eg :- 21:01:01
Instead of concatenating strings, which is highly inefficient, you'll want to use String.Format. This also supports Custom Date and Time Format Strings, which make it trivial to achieve what you want.
Related
I am making a databse for a sports club,
When filling out a form, they input the facility ID, start time, end-time and date.
What I am trying to do is when they enter the end time box, the function scans through the entries on the 'Bookings' Table where all the data from this form is stored, to see if the facility is booked out at this time. ( For dtermining if it is booked out at a certain time, if the start time or the end time on table is between what is filled in on the form, an error is thrown
The code is shown below:
Private Sub EndNon_AfterUpdate()
Dim criteria As String
criteria = _
"Non-PlayingFacilityID= " & Me.NonPlayID.Value & " " & _
"Date(Non-PlayingFacility)= " & Me.DateNon.Value & _
" " & "AND [StartTime(Non-PlayingFacility)] Between Me.StartNon.Value And Me.EndNon.Value OR [EndTime(Non-PlayingFacility)] Between Me.StartNon.Value And Me.EndNon.Value "
If DCount("*", "Bookings", criteria) > 0 Then
MsgBox "Unfortunately, this facility is booked at this time"
Me.Undo
End If
End Sub
Syntax error is thrown when I run this, not sure why.
Any help would be much appreciated
It probably highlights this invalid syntax:
"Date(Non-PlayingFacility)= " & Me.DateNon.Value & _
Perhaps you mean:
"DateValue([Non-PlayingFacility])= " & Format(Me!DateNon.Value, "\#yyyy\/mm\/dd\#") & _
The remaining date comparisons need to be concatenated and formatted similarly.
Addendum - given the obscure field name:
"[Date(Non-PlayingFacility)]= " & Format(Me!DateNon.Value, "\#yyyy\/mm\/dd\#") & _
The format is needed to create a valid string expression in SQL for the date value. This is independent of a format applied for display.
I have this script to get the data less than or equal the time 11:59:00 AM:
Me.lbl001.Caption = Format(DLookup("DateTime", "tblTime", "[ID]=" & Me.txtMacID & " AND [DateTime]<=#5/2/2017 11:59:00 AM#), "hh:mm:ss")
But it affects the Date so I'm also getting the data of 5/1/2017 and less. I'm trying to achieve getting data less than 11:59:00 AM within 5/2/2017 only.
Try this:
Format(DMax("DateTime", "tblTime", "[ID]=" & Me.txtMacID & " AND DateValue([DateTime])=#2/5/2017#"), "hh:mm:ss")
A2=201604
I am trying to pick up value from a Cell(Date) and use the same for saving the file.
But while saving instead of the date a value is being printed, see code below:
Dim part1 as string
part1 = Range("A2").Value
ActiveWorkbook.SaveAs Filename:=ThisWorkbook.Path & "\xxx- " & Format(part1, "MMM-YYYY") & ".xlsx" , FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
201604 is not a date. It is just a number.
You could use something like
myDate = "01/" & right(a2,2) & "/" left(a2,4)
string yourAnswer = format(myDate, "MMM-YYYY")
The problem is you're prematurely casting the Date into a String by using the part1 variable, so the 'date' is already broken and not recognisable as a date by the time it hits your Format() call.
Either declare part1 as a Date or just skip the whole part1 declaration and insert Range('A2').Value directly into the Format() call.
I want to generate report from 01-Feb-2016 to 31-Mar-2016. Using formula
{cryRpt.RecordSelectionFormula = "{product_details.date11} >= #" &
DateTimePicker1.Text & "# and {product_details.date11} <= #" &
DateTimePicker2.Text & "#"}
but it is still not providing proper sequence, sometime places 01-Mar-2016 in between, same with 29-Feb-2016, using group in crystal report but still not success, help me out
Between these # crystal treats as datetime and not date values to get correct values you need to convert to date try like this:
{cryRpt.RecordSelectionFormula = "{product_details.date11} >=CDate( #" &
DateTimePicker1.Text & "#) and {product_details.date11} <= CDate(#" &
DateTimePicker2.Text & "#"})
I haven't checked the syntax, check when you are using in record selection formula
I tried doing
senddate.Value = Format(senddate.Value, "mm/dd/yyyy")
sstartdate.Value = Format(sstartdate.Value, "mm/dd/yyyy")
MsgBox(senddate.Value & " " & sstartdate.Value)
but the messagebox still returns the time as well. (message box is there for diagnostic purposes, though it also passes the time to the SQL string as well.)
I tried switching the date time pickers to custom format and it didn't work. Any ideas?
I need to turn it into a short, simple date so that I can input it into an SQL Access BETWEEN query
senddate.Value = Format(senddate.Value, "MM/dd/yyyy")
sstartdate.Value = Format(sstartdate.Value, "MM/dd/yyyy")
MsgBox(senddate.Value & " " & sstartdate.Value).ToString
the month part should be in capital lettrs.