Dim DespatchDate As DateTime
Dim ReceiptDate As DateTime
DespatchDate = #3/7/2017 12:00:00 AM# 'I am in the UK so this is 3rd July 2017
ReceiptDate = DespatchDate.AddDays(60) 'Returns #5/6/2017 12:00:00 AM#
I would expect "1/9/2017" the 1st September 2017
I also tried
ReceiptDate = DateAdd("d", 10, DespatchDate) which returned #3/17/2017 12:00:00 AM#
I must be doing something wrong but what?
The date literal in VB uses the US format regardless of where you are. Your code:
DespatchDate = #3/7/2017 12:00:00 AM# 'I am in the UK so this is 3rd July 2017
is creating the date March 7 2017. Read more on date literals or use the constructor on the date class that makes it more apparent what you are doing.
Update
In order to be sure what the date is and to avoid ambiguity, you can define it this way:
DespatchDate = New Date(2017, 7, 3) ' Without the time of day, 12AM is implied
DespatchDate = New Date(2017, 7, 3, 12, 34, 56) ' July 3 2017 12:34:56 PM
The good thing with this is you can see from intellisense while typing the code what each number means. And if someone else had written the code, you can invoke intellisense to see what each argument means. As you can see from what I highlighted in my screenshot, the time is in 24 hour format.
Related
I have a Access table which contains the Strings like the following:
Date Created Date Modified
January 31, 2019 January 31, 2019
March 08, 2019 March 09, 2019
April 19, 2019 April 23, 2019
I want to be able to select a Date Range between 2 dates using a sql query.
I am using the Jet4.0 engine and VB6.
Any help is greatly appreciated
I have tried this:
But this does not delete any records.
Set cijb = DBEngine.Workspaces(0).OpenDatabase(PathCIJB())
DELETE Job.* FROM Job WHERE(Job.DateModified >= 'January 03, 2019' ) AND
(Job.DateModified <= 'February 05, 2019' )
Set rs = cijb.OpenRecordset(sql, dbOpenSnapshot)
I expect to delete all the records Modified for example from
March 09, 2019 to April 23, 2019
You comparing strings with strings. And there is no conditions when 'J' comes before 'F', so WHERE is always false.
Before compare you should do explicit convertation by CDate function:
CDate recognizes date formats according to the locale setting of your
system. The correct order of day, month, and year may not be
determined if it is provided in a format other than one of the
recognized date settings. In addition, a long date format is not
recognized if it also contains the day-of-the-week string.
Also to run SQL command query you can not use OpenRecordset:
Set cijb = DBEngine.Workspaces(0).OpenDatabase(PathCIJB())
cijb.Execute "DELETE Job.* FROM Job " & _
"WHERE (CDate(Job.DateModified) >= CDate('January 03, 2019')) AND (CDate(Job.DateModified) <= CDate('February 05, 2019'));"
With a Date variable, apparently it defaults to January 1, 0001 if you don't enter a date in a date/time literal.
However, if I choose to display that default date, it shows the date December 30, 1899 which isn't even an acceptable date in Excel. Here's the code:
Sub TimeOfBirth()
Dim BirthTime As Date
BirthTime = "2:00:00 PM"
MsgBox Format(BirthTime, "yyyy-mm-dd")
End Sub
Can anyone explain this? I get that VBA stores a much greater range of dates, but I don't understand why December 30, 1899 is being displayed?
What's the math/logic there?
Excel treats date as a number. Since you provide only Time, he assumes that Date = 0.
And Date 0 is December 30, 1899.
Edit
To answer your question from comments:
Date defaults to January 1, 0001 in VB.NET.
Date default to December 30, 1899 in VBA and is stored as 0.
Date = 1 not equals to January 1, 1900, but to December 31, 1899. January 1, 1900 is equal to 2. Check code below.
Sub TimeOfBirth()
MsgBox Format(0, "yyyy-mm-dd") '1899-12-30
MsgBox Format(1, "yyyy-mm-dd") '1899-12-31
MsgBox Format(2, "yyyy-mm-dd") '1900-01-01
End Sub
I have this string: 28 June 2018 (22:05)
How can I compare it with my current time and get the difference?
For example if actual time was 29/06/2018 (05:49)
The difference will be: 7 hours 44 minutes
So input: 28 June 2018 (22:05)
Output: 7 hours 44 minutes
The first thing you need to do, is convert the string to a valid DateTime instance.
If you know your dates will always be in this format, you can do the following...
Dim mydate = DateTime.ParseExact("28 June 2018 (22:05)", "dd MMMM yyyy (HH:mm)", CultureInfo.InvariantCulture)
https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
Once you've parsed the string into a valid DateTime instance, you can use all the normal date functions to do the comparisons.
I would first get the difference in minutes, like so...
Dim diffminutes = DateDiff(DateInterval.Minute, mydate, Now)
Then create a timespan like this...
Dim mytimespan = TimeSpan.FromMinutes(diffminutes)
Finally display the difference in hours and minutes like this...
Response.Write(mytimespan.ToString("hh\:mm"))
im doing my activity dtr system and i want to know how to determine whether it am or pm? 7am - 12:30 pm should go on AM, and 1pm - 5PM will go to pm.
for example:
if the time is 12:30 pm its still go to am and if its 12:31 pm it will go now to pm.
Dim hour As Date = TimeOfDay.ToString("hh:mm")
If hour <= "12:30" Then
isTimeAM_Exist() ' will go to am
Else
isTimePM_Exist() ' will go to pm
End If
in my code 4:00 pm still go in am.
thankyou in advance
Instead of converting to string, work directly with the DateTime structure, and compare it with a correct starting value
Dim now = Date.Now
If now < New DateTime(now.Year, now.Month, now.Day, 12, 00, 0) Then
'It's currently AM
Else
'It's currently PM
End If
sorry when I do not understand your question, but TimeOfDay HAS this Information:
Dim hour As Date = TimeOfDay
If hour.Hour >= 12 Then
' do whatever you like to do on afternoon
Else
' time to wake up
End If
had to test this first in my ide
This will allow you to define your own 'AM' and 'PM':
' FormatDateTime(Now(), 4) gives 24-hour format (hh:mm)
If Hour(FormatDateTime(Now(), 4)) + Minute(FormatDateTime(Now(), 4))/60 <= 12.5 Then
' Is AM
Else
' Is PM
End If
maybe this way is more effective
MsgBox(DateTime.Now.AddMinutes(-30).ToString("tt"))
I'm having a bit of trouble with the mongodb c# driver, in that it seems to be converting all my dates to a UTC form.
I have
Dim cDate as Date
Dim year as integer = 2012
Dim month as integer = 12
Dim day as integer = 21
cDate = New Date(year, month, day)
However putting it into a mongodb database via the C# driver seems to convert it to UTC so all of a sudden its a different day now because its now 11:00 PM 20th December 2012. Not exactly what I wanted!
Is there a way I can create the New Date(year, month, date such that its an UTC mode to begin with? So if i did cDate.utcNow I would get the same thing as cDate, in essence cDate.utcNow = cDate?
I have tried all sorts of stuff with the driver only to run into a brick wall such as using the DateTimeSerializationOptions.Defaults to no avail nothing happens!
To create your date as a UTC date, just specify the Kind parameter in the constructor along with the hours, minutes, and seconds:
cDate = New Date(year, month, day, 0, 0, 0, DateTimeKind.Utc)