I have a function that returns the online date and time but for some reason the minutes are not working. When I add a value to the minutes it always stays the same and won't move forward.
Public Function OnlineTimeNow() As DateTime
' --- create instance of UDP
Dim objSck As System.Net.Sockets.UdpClient
Dim ipAny As System.Net.IPEndPoint = New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
objSck = New System.Net.Sockets.UdpClient(ipAny)
' --- send UDP
Dim sdat As Byte() = New Byte(47) {}
sdat(0) = &HB
objSck.Send(sdat, sdat.GetLength(0), "time.windows.com", 123)
' --- receive UDP
Dim rdat As Byte() = objSck.Receive(ipAny)
' elapsed time (date and time) from 1900/01/01 ---
Dim elapsedTotalSec As Long ' elapsed seconds from 1900/01/01 ---
Dim Days As Long ' days --- 日
Dim HH As Long ' hours --- 時
Dim MM As Long ' minutes --- 分
Dim SS As Long ' seconds --- 秒
' --- elapsed seconds from 1900/01/01 ---
elapsedTotalSec = CLng(
rdat(40) * Math.Pow(2, (8 * 3)) +
rdat(41) * Math.Pow(2, (8 * 2)) +
rdat(42) * Math.Pow(2, (8 * 1)) +
rdat(43))
' ---
Days = elapsedTotalSec \ (24 * 60 * 60) ' days
SS = elapsedTotalSec Mod (24 * 60 * 60) ' mod seconds
HH = SS \ (60 * 60)
SS = SS Mod (60 * 60)
MM = MM \ (60 * 60)
SS = SS Mod 60
' --- convert to DateTime type
'Dim dtTime As DateTime = "1900/01/01"
Dim dtTime As DateTime
'dtTime = dtTime.AddDays(Days)
dtTime = dtTime.AddHours(HH)
dtTime = dtTime.AddMinutes(MM)
dtTime = dtTime.AddSeconds(SS)
' --- change Greenwich Mean Time to local time (my sample is Japanese Time)
dtTime = dtTime.AddHours(8) ' <<-- *** please modify (n) according to your location ***
' ---
Return dtTime
End Function
If you want to get the GTM time, you can do it simply with :
Dim dtTime As DateTime
dtTime = DateTime.UtcNow
This is an exemple on how to convert Utc time to another time zone :
Sub Main()
Dim DT As DateTime = DateTime.UtcNow
Dim DTSEAsia As DateTime
Dim Tz As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("SE Asia Standard Time")
DTSEAsia = TimeZoneInfo.ConvertTimeFromUtc(DT, Tz)
End Sub
And this are the value possible for a TimeZoneInfo
Dateline Standard Time
UTC-11
Samoa tandard Time
Hawaiian Standard Time
Alaskan Standard Time
Pacific Standard Time (Mexico)
Pacific Standard Time
US Mountain Standard Time
Mountain Standard Time (Mexico)
Mountain Standard Time
Central America Standard Time
Central Standard Time
Central Standard Time (Mexico)
Canada Central Standard Time
SA Pacific Standard Time
Eastern Standard Time
US Eastern Standard Time
Venezuela Standard Time
Paraguay Standard Time
Atlantic Standard Time
Central Brazilian Standard Time
SA Western Standard Time
Pacific SA Standard Time
Newfoundland Standard Time
E.South America Standard Time
Argentina Standard Time
SA Eastern Standard Time
Greenland Standard Time
Montevideo Standard Time
UTC-2
Mid-Atlantic Standard Time
Azores Standard Time
Cape Verde Standard Time
Morocco Standard Time
UTC
GMT Standard Time
Greenwich Standard Time
W.Europe Standard Time
Central Europe Standard Time
Romance Standard Time
Central European Standard Time
W.Central Africa Standard Time
Namibia Standard Time
Jordan Standard Time
GTB Standard Time
Middle East Standard Time
Egypt Standard Time
Syria Standard Time
South Africa Standard Time
FLE Standard Time
Israel Standard Time
E.Europe Standard Time
Arabic Standard Time
Arab Standard Time
Russian Standard Time
E.Africa Standard Time
Iran Standard Time
Arabian Standard Time
Azerbaijan Standard Time
Mauritius Standard Time
Georgian Standard Time
Caucasus Standard Time
Afghanistan Standard Time
Ekaterinburg Standard Time
Pakistan Standard Time
West Asia Standard Time
India Standard Time
Sri Lanka Standard Time
Nepal Standard Time
Central Asia Standard Time
Bangladesh Standard Time
N.Central Asia Standard Time
Myanmar Standard Time
SE Asia Standard Time
North Asia Standard Time
China Standard Time
North Asia East Standard Time
Singapore Standard Time
W.Australia Standard Time
Taipei Standard Time
Ulaanbaatar Standard Time
Tokyo Standard Time
Korea Standard Time
Yakutsk Standard Time
Cen.Australia Standard Time
AUS Central Standard Time
E.Australia Standard Time
AUS Eastern Standard Time
West Pacific Standard Time
Tasmania Standard Time
Vladivostok Standard Time
Central Pacific Standard Time
New Zealand Standard Time
UTC+12
Fiji Standard Time
Kamchatka Standard Time
Tonga Standard Time
Just To Post a Simple Method for the Next User "Online Date and Time VB.NET"
Private Sub OnlineDateAndTime()
Dim client As WebClient = New WebClient()
'Download The TextFile From Web.
'Refer To "https://worldtimeapi.org/" about the link below
Dim DateAndTimeFromWeb = DateTimeOffset.Parse(client.DownloadString("https://worldtimeapi.org/api/timezone/Asia/Manila.txt").Split(ChrW(10)).Skip(2).First().Substring(10))
Dim TimeNow As DateTime
Dim DateNow As DateTime
'Get Only Time
TimeNow = DateAndTimeFromWeb.ToString
TimeNow = TimeOfDay.ToString("h:mm:ss tt")
'Get Only Date
DateNow = DateAndTimeFromWeb.ToString
DateNow = DateTime.Now.ToString("dd/MM/yyyy")
'Show Results in Textbox or Label or Any
TextBox1.Text = DateNow
TextBox2.Text = TimeNow
End Sub
Credits to #Jimi - Thanks ^_^
Related
What's the best way to show a LocalTime value based on the device? The United States is the only nation that exclusively uses the AM/PM time of day abbreviation but for some reason the abbreviation does not appear whenever my device locale is set to the United States.
United States
2:30 AM / 2:30 PM
Elsewhere around the world
02:30 / 14:30
Current code
myTV.text = LocalTime.of(2, 30) + " " + LocalTime.of(14, 30)
Current result
2:30 14:30
You need to format your times explicitly. Use DateTimeFormatter.ofLocalizedTime.
DateTimeFormatter localizedTimeFormatter
= DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT);
LocalTime begin = LocalTime.of(2, 30);
LocalTime end = LocalTime.of(14, 30);
String text = begin.format(localizedTimeFormatter) + ' ' + end.format(localizedTimeFormatter);
System.out.println(text);
Sorry that I can write only Java code. When I ran the code on my desktop Java 9 with default locale set to US, the output was:
2:30 AM 2:30 PM
Output with other locales:
Canada: 2:30 AM 2:30 PM
UK or France: 02:30 14:30
I writing application using vb .net.
In my application I got text box displaying current date.
As usually after midnight date is changing.
There is any chance to set delay that date will change after 02:00 am next day ?
For example:
Today is 09/07/17 and I need that after midnight textbox will still show this date. But after 02:00 am ( 10/07/117 ) date will change for 10/07/17.
My current code:
Private Sub HOMESCREEN_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SMARTSCREEN.MdiParent = Me
SMARTSCREEN.StartPosition = FormStartPosition.CenterScreen
SMARTSCREEN.Show()
SMARTSCREEN.datee.Text = Date.Today()
SMARTSCREEN.NOWEEK.Text = (DatePart("WW", Now))
Thanks,
The big (actually huge) question is, in what time zone is that 2:00 am you're talking about? You can easily achieve that by simply changing the time zone.
Say your time zone is US Eastern Time -5, then setting the time zone in your code to US Mountain Time -7 will effectively give you the result you want.
EDIT: This is trivial, but since you mentioned that you're in UK, then I assume that your time zone is London 0, so set to Coordinated Universal Time -2 to achieve what you want.
Alternatively, it can be simpler in your code to just subtract two hours:
SMARTSCREEN.datee.Text = DateTime.Now.AddHours(-2).ToString("d")
I would like to check if any specified timezone (other than my local time zone) is in DST or not.
To check if the local time zone is currently DST I am using:
Dim today_date As Date = Now.Date
Msgbox(today_date.IsDaylightSavingTime())
I would like to check if other time zones ("A.U.S. Eastern Standard Time" and "New Zealand Standard Time") are in DST according to today's date.
So what I have so far:
Dim AusZoneId As String = "A.U.S. Eastern Standard Time"
Dim AusZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(AusZoneId)
MsgBox(AusZone.SupportsDaylightSavingTime)
'Established True or False if TZ supported DST
What I want to do is feed in a date (eg today) and see if this date according to AUS Eastern Standard time is in DST.
You shouldn't do it for a date, as that can be ambiguous (the start of the day in standard time and the end of the day in daylight time, or vice versa) but instead ask whether a specific point in time is in daylight time:
Dim zoneId As String = "AUS Eastern Standard Time";
Dim zone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(zoneId)
Dim dst As Boolean = zone.IsDaylightSavingTime(DateTime.UtcNow)
(Or use some other point in time, of course.) I'd strongly encourage you to pass in a DateTime with a Kind of Utc.
I want to get timestamp of a different country in VBA. Is there any direct function or way to get it? For example, I am in India working for Mexico and I want to do certain task based on Mexican Time. I was able to get it by splitting the timestamp and manipulating it but could not consider daylight saving in it. Is there any simple solution than writing a big user-defined function?
Know your time zone offset & your client's time zone offset, then use that to calculate the difference
Dim IndiaTZ as single
Dim MexicoTZ as single
Dim MyTime as date
Dim UTC as date
Dim MexicoTime as date
IndiaTZ = 5.5
MexicoTZ = -4
'note this assumes that they're in Eastern time,
'Mexico also covers Central, Mountain & Pacific at -5, -6 & -7.
'You'll need to figure out which one you need.
MyTime = Now
'need to invert the offset to get from India to UTC
UTC = datediff("h", mytime, IndiaTZ * -1)
'need to invert the offset to get from UTC to Mexico
MexicoTime = Datediff("h", UTC, MexicoTZ * -1)
I've been working on parsing strings and I have a test case that has been causing problems for me. When parsing a date/time string with strptime, Daylight Savings Time is NOT accounted for. This is a bug as far as I can tell. I can't find any docs on this bug. Here is a test case in the Rails console. This is ruby 1.9.3-p215 and Rails 3.2.2.
1.9.3-p125 :049 > dt = DateTime.strptime("2012-04-15 10:00 Central Time (US & Canada)", "%Y-%m-%d %H:%M %Z")
=> Sun, 15 Apr 2012 10:00:00 -0600
1.9.3-p125 :050 > dt = DateTime.strptime("2012-04-15 10:00 Central Time (US & Canada)", "%Y-%m-%d %H:%M %Z").utc
=> Sun, 15 Apr 2012 16:00:00 +0000
1.9.3-p125 :051 > dt = DateTime.strptime("2012-04-15 10:00 Central Time (US & Canada)", "%Y-%m-%d %H:%M %Z").utc.in_time_zone("Central Time (US & Canada)")
=> Sun, 15 Apr 2012 11:00:00 CDT -05:00
As you can see, I have to convert to utc and then back to the timezone to get DST to be properly interpreted, but then the time is shifted one hour as well, so it's not what I parsed out of the string. Does someone have a workaround to this bug or a more robust way of parsing a date + time + timezone reliably into a DateTime object where daylight savings time is properly represented? Thank you.
Edit:
Ok, I found a workaround, although I'm not sure how robust it is.
Here is an example:
ActiveSupport::TimeZone["Central Time (US & Canada)"].parse "2012-04-15 10:00"
This parses the date/time string into the correct timezone. I'm not sure how robust the parse method is for handling this so I'd like to see if there is a better workaround, but this is my method so far.
This is a frustrating problem. The Rails method you're looking for is Time.zone.parse. First use DateTime.strptime to parse the string, then run it through Time.zone.parse to set the zone. Check out the following console output:
> Time.zone
=> (GMT-06:00) Central Time (US & Canada)
> input_string = "10/12/12 00:00"
> input_format = "%m/%d/%y %H:%M"
> date_with_wrong_zone = DateTime.strptime(input_string, input_format)
=> Fri, 12 Oct 2012 00:00:00 +0000
> correct_date = Time.zone.parse(date_with_wrong_zone.strftime('%Y-%m-%d %H:%M:%S'))
=> Fri, 12 Oct 2012 00:00:00 CDT -05:00
Notice that even though Time.zone's offset is -6 (CST), the end result's offset is -5 (CDT).
Ok, here is the best way I've found to handle this so far. I created a utility method in a lib file.
# Returns a DateTime object in the specified timezone
def self.parse_to_date(date_string, num_hours, timezone)
if timezone.is_a? String
timezone = ActiveSupport::TimeZone[timezone]
end
result = nil
#Chronic.time_class = timezone # Trying out chronic time zone support - so far it doesn't work
the_date = Chronic.parse date_string
if the_date
# Format the date into something that TimeZone can definitely parse
date_string = the_date.strftime("%Y-%m-%d")
result = timezone.parse(date_string) + num_hours.to_f.hours
end
result
end
Note that I add hours onto the time manually because Chronic.parse wasn't as robust as I liked in parsing times - it failed when no trailing zeros were added to a time, for example, as in 8:0 instead of 8:00.
I hope this is useful to someone. Parsing date/time/timzone strings into a valid date seems to be a very common thing, but I was unable to find any parsing code that incorporated all three together.