Does DateTime include second leap function? - vb.net

In this article: Are .Net's DateTime methods capable of recognising a Leap Second?, the writer gets the answer "It (DateTime) does not include the number of ticks that are attributable to leap seconds."
However, the following code shows that .Net DateTime supports leap seconds. Could any expert explain why?
Dim GPS_Time_Base_Point As DateTime = New DateTime(1980, 1, 6, 0, 0, 0, DateTimeKind.Unspecified)
Dim A_test_time As DateTime = New DateTime(2019, 4, 16, 18, 29, 0, DateTimeKind.Unspecified)
Dim TimeSpan As TimeSpan = A_test_time - GPS_Time_Base_Point
Dim GPS_Time_At_LIGO_for_2019_4_16_18_29_0 As UInt64 = 1239474558 'https://www.andrews.edu/~tzs/timeconv/timeconvert.php
Dim DifSeconds As Integer = TimeSpan.TotalSeconds - 1239474558
Debug.Print(" Second dif:" + CDbl(DifSeconds).ToString)
The output is "Second dif:-18", which is just equal to the leap second dif for gpstime - UTC time.

Related

subtract from one date another

Good day. There is the following code that subtracts from one date another:
Dim dt As New DateTime(2021, 8, 19)
Dim dt2 As New DateTime(2022, 8, 29)
MsgBox("Days Remaining : " & (dt2 - dt).Days)
tell me how to subtract format dates:
19.08.2021 14:07:16 - 24.08.2021 08:24:01
throwing away the time. I'm only interested in how to subtract dates of this format.
Date and time are entered in text boxes
You would get the Date property (documentation) of the DateTime values. Also, I would probably recommend using DateTime.Subtract (documentation) which would return a TimeSpan value.
Take a look at this example:
Dim dt = New DateTime(2021, 8, 19)
Dim dt2 = New DateTime(2022, 8, 29)
Dim daysLeft = dt2.Subtract(dt).Days
Example: https://dotnetfiddle.net/LMuWtu
You have to parse the formatted Dates then use .Date to keep only the Date part and throw away the time. Then you can substract and use TotalDays:
Dim input1 As String = "19.08.2021 14:07:16"
Dim input2 As String = "19.08.2021 14:07:16"
Dim d1 As DateTime = DateTime.Parse(input1).Date
Dim d2 As DateTime = DateTime.Parse(input1).Date
Dim days As Single = (d1 - d2).TotalDays
The formatted dates should match your Culture format. You can also use Parse overload to use another Culture : https://learn.microsoft.com/fr-fr/dotnet/api/system.datetime.parse?view=net-5.0#System_DateTime_Parse_System_String_System_IFormatProvider_

How To Declare / Store "12:00:00 AM" in a DateTime Variable? (Through military time)

I have this code:
Dim twelve_AMToday As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 24, 0, 0)
But it says "Hour, Minute, and Second parameters describe an un-representable DateTime."
Of course 23, 59, 59 is read as 11:59:59 PM. I've tried 0, 0, 0 but then gives me the date only.
Thanks for any help!
You get it when you use Today
Dim twelve_AMToday As DateTime = Today
If you want to add days put this code under it.
twelve_AMToday = twelve_AMToday.AddYears(0).AddMonths(0).AddDays(1)
Try this
Dim twelve_AMToday As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0)
Dim s As String = twelve_AMToday.ToLongDateString & " " & twelve_AMToday.ToLongTimeString
What you see in the debugger will be the same for the datetime, but once the datetime is converted to a string you will see that it does have the time you want. If the time is all zeroes the debugger doesn't show the time for datetimes, but it is there.

How can i retrieve date time available per second in one day?

my input is date.
But, i'm stuck on how to retrieve date time in every second.
I need to put the each second date time in the 2d array.so my array(0,0) should equal to 2/10/2014 00:00:00 AM and array(86399,0) is equal to 2/10/2014 23:59:59 PM.
i tried do looping as per below code:
Dim twoDarray(86399, 1) As String
Dim dtInput As Date
dtInput= #2/10/2014#
For i=0 to 86399
twoDarray(i, 0) = dtInput
dtInput = dtInput +second 'i know this not right
Next
I just don't know how to increase date time every second in right way.
Please help.
Have you thought about something along the lines of
Using a Datetime (MSDN Datetime)
dtInput= new DateTime(2014,10,2)
For i=0 to 86399
twoDarray(i, 0) = dtInput
dtInput = dtInput.AddSeconds(1)
Next
Or
dtInput= new DateTime(2014,10,2)
For i=0 to 86399
twoDarray(i, 0) = dtInput.AddSeconds(i+1)
Next
You can try following method also
Dim dtFrom As New DateTime(2014, 10, 2, 0, 0, 0)
Dim dtTo As New DateTime(2014, 10, 2, 23, 59, 59)
Dim iFirstDim As Integer = (dtTo - dtFrom).TotalSeconds
Dim iSecondDim As Integer = 10
Dim arrTime(iFirstDim, iSecondDim) As String
Dim i As Integer = 0
Do While (dtTo > dtFrom)
arrTime(i, 0) = dtFrom.ToString("d/MM/yyyy HH:mm:ss")
dtFrom = dtFrom.AddSeconds(1)
i += 1
Loop
HOW TO USE IT?
Dim dtResult As DateTime
If DateTime.TryParseExact(arrTime(150, 0), "d/MM/yyyy HH:mm:ss", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, dtResult) Then
MsgBox(dtResult.ToString("yyyy-MM-dd HH:mm:ss"))
End If

How to set only time part of a DateTime

I need to set datetime variable but it's time part must be 3:00:00 AM.
For example if i call getdate() now i'll get 1/20/2014 3:52:03 AM. I need to set it to 1/20/2014 3:00:00 AM.
Does anybody have a good snippet for that or any ideas how to do it right? would really appreciate some help.
The constructor for the DateTime allows you to specify hours, minutes and seconds as well as the usual day month and year.
Dim now As DateTime = DateTime.Now
Dim myDate = New DateTime(now.Year, now.Month, now.Day, 3, 0, 0, 0)
There are a lot of ways to do this - reading the documentation might give you more ideas
Dim someDate As DateTime = DateTime.Now()
Dim threeAmDate As DateTime = New DateTime(someDate.Year, _
someDate.Month, _
someDate.Day, _
3, _ 'hours
0, _ 'minutes
0, _ 'seconds
0) 'milliseconds
Console.WriteLine(threeAmDate)
try this
Label1.Text = DateAndTime.Today & " " & TimeOfDay
you can put this in a label or anything and you can change time of day to "3:00:00"
This one is old... But I came across having the same question.
Dim dt As New DateTime(_DateTo.Ticks + _TimeTo.Ticks)
--> Both '_DateTo' and/or '_TimeTo' could be Timespan or Datetime objects.

Best way to convert total minutes into HH:mm format?

I get a return value from a web service in minutes, for example 538 minutes. I need to break this down in hours and minutes. What is the fastest way, in .net code and also VB6 code (two apps use the service) to convert this from minutes to HH:mm?
Thanks
This code should work both in .NET and VB6:
Dim hours As Integer = 538 \ 60
Dim minutes As Integer = 538 - (hours * 60)
Dim timeElapsed As String = CType(hours, String) & ":" & CType(minutes, String)
label1.Text = timeElapsed
In .NET exclusively, you should be able to do the following (which requires to be tested):
Dim timeElapsed As DateTime = New DateTime(1, 1, 1, 0, 538, 0)
label1.Text = timeElapsed.ToString("HH:mm")
I hope this helps!
In .Net you have a TimeSpan class so you can do the following
Dim t As New TimeSpan(0, 538, 0)
'Then you have the 2 properties
t.Hours
t.Minutes
In VB6 you could just use Format(538/1440.0, "hh:mm")
VB6 Date values can be treated as a number of days, and there's 1440 minutes in a day. So 538/1440 is the number of days in your period, and then you can use Format
Take modulus 60, then integer-divide by 60.
In VB, integer division uses \.
If you need to perform operations with other DateTime objects it might be useful to use a TimeSpan object instead, e.g.
Dim oTS As New TimeSpan(0, 538, 0)
MessageBox.Show(Format(oTS.Hours, "00") & ":" & Format(oTS.Minutes, "00"))
Dim startime As DateTime = Date.Now
Dim newtime As DateTime = startime + oTS
MessageBox.Show(newtime.ToString("HH:mm"))
If not then Matthew's suggestion of using Integer division '\' and modulo 'Mod' will work very well.
if you need strings then:
dt.ToString("HH:mm");
(info)