I'm using TimeSpan function in VB.NET to get the total hours in a billing month.
I have 30 days in a billing month, so supposedly, I have 720 intervals. Using Timespan normally, I would get 696 intervals. But I add a day in my "todate" in order to get my correct total hours.
This is my code:
Dim TS As TimeSpan
Dim totalhours As Integer
Dim todate As DateTime = #11-30-2014#
Dim fromdate As DateTime = #11-01-2014#
TS = todate.AddDays(1).Subtract(fromdate)
totalhours = TS.TotalHours
However, I don't think that manipulating my input date (by adding a day) is the best practice. Is there any way to configure the timespan function to get the inclusive total hours?
That is because you did not supply the time range. The date alone will be 12AM for the time part.
Dim TS As TimeSpan
Dim totalhours As Integer
Dim todate As DateTime = #11-30-2014 11:59:59 PM#
Dim fromdate As DateTime = #11-01-2014#
TS = todate.Subtract(fromdate)
totalhours = TS.TotalHours
That will be 1 hours shy.
Dim TS As TimeSpan
Dim totalhours As Integer
Dim todate As DateTime = #12-01-2014#
Dim fromdate As DateTime = #11-01-2014#
TS = todate.Subtract(fromdate)
totalhours = TS.TotalHours
Will get the correct amount since it is inclusive of time between the two dates.
Related
I'm trying to format a timestamp 1624373961042 to a date but the way I've tried below always gives me overflow error. I was expecting to get this date 2021-06-22 from that timestamp. How can I make it possible?
Sub getDate()
Dim tDate As Long, timestamp As String
timestamp = "1624373961042"
tDate = Int(timestamp) / 1000
MsgBox Format(tDate, "yyyymmdd")
End Sub
You need to divide the number by the number of milliseconds in a day: 86400000
then add the number of days from 12/31/1899 and 1/1/1970: 25569
Sub getDate()
Dim tDate As Long, timestamp As String
timestamp = "1624373961042"
tDate = Int((CDbl(timestamp) / 86400000) + 25569)
MsgBox Format(tDate, "yyyymmdd")
End Sub
When reading DateTime from excel, the result day and month are being read as dd MM, while the excel content was according to MM dd style.
S1 contains: "12/09/2017"
The code:
Dim t_from As DateTime
t_from = CDate(s1)
t_from includes Sep as a month, instead of Dec as should be.
I also tried:
Dim b As Boolean = DateTime.TryParseExact(s1, "MM/dd/yy",
System.Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.None,dt)
this code fails (String was not recognized as a valid DateTime)
How can I convert the text to DateTime VB variable, according to month first (before date)?
Format "M/d/yyyy h:m:s tt" should parse correctly dates from the excel
Dim parsedDate As Date
Date.TryParseExact(
"9/12/2017 12:00:00 AM",
"M/d/yyyy h:m:s tt",
Globalization.CultureInfo.InvariantCulture,
Globalization.DateTimeStyles.None,
parsedDate)
parsedDate.ToString() ' 12.09.2017 00:00:00
For "12/09/2017" where 12 is a month and 09 is a day use format: "MM/dd/yyyy"
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"))
I have been wracking my brain for days..
How do you take a datetimepicker box value and convert the 24hr time format HH:mm:ss to complete total unit seconds?
Basically if the datetimepicker is set to 01:01:01 The answer I am looking for would be 3661
Thank you for any help and reply.
Try this:
Dim dt As DateTime = Now()
Dim d2 As DateTime = Now.Date
Dim ts As TimeSpan = dt.Subtract(d2)
Dim s As String = ts.TotalSeconds
Get time by using Now for dt.
Get only the date for d2 which renders the time 00:00:00.
Subtract the lesser and voila :)
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)