How to check DST status for specified timezones - vb.net

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.

Related

SQL time in incorrect from that of my system timezone

We have a SQL database that returns all the times in Greenwich Mean Time (GMT). We are in the Eastern Standard Timezone (EST). This messes up some queries that we have that pull data from specific dates. I tried using the (StartTime AT TIME ZONE 'Eastern Standard Time' as StartTime_ET, but that only returns the result as same in GMT -5. I just want the exact result to be in EST .
This changes the complete logic process of mine. Is there any way to do that?
Assuming your values are all UTC and that your column StartTime is not a datetimeoffset, then you need to turn your value into a datetimeoffset first, and then change the time zone. When you use AT TIMEZONE on a date and time data type that isn't a DATETIMEOFFSET it is assumed that the value is already at the correct timezone. Therefore, for example something like SELECT GETUTCDATE() AT TIME ZONE 'Eastern Standard Time'; would return 2021-08-05 09:53:56.8500000 -04:00 right now, even though the time in EST is actually 2021-08-05 05:53:56.8500000 -04:00 right now.
As a result you need to add the offset first and then use AT TIME ZONE. So, with GETUTCDATE that would be like this:
SELECT SWITCHOFFSET(GETUTCDATE(),0) AT TIME ZONE 'Eastern Standard Time';
Therefore, presumably, you just need to do the same for your column, StartTime, which is also a UTC time:
SWITCHOFFSET(StartTime,0) AT TIME ZONE 'Eastern Standard Time'
If you don't want the timezone portion, then you can convert it back to a different date and time data type:
CONVERT(datetime2(0),SWITCHOFFSET(StartTime,0) AT TIME ZONE 'Eastern Standard Time')

VB.Net Online Time

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 ^_^

convert local time to utc and add to utc datetime moment

I have a user input where the user enters year, month and day and I create a date object
let userDate = new Date(year, month, day)
and then from a user input where the user enters minutes and hours (into variables minutes and hours)
I need to convert this date and time into UTC timestamp, using moment and I'm not sure about it. Can I just do
let utcTimestamp = moment.utc(date.toISOString()).hour(hours).minutes(minutes);
For example: if a user enters a date of 13-Mar-2018 and time of 8:45 AM (in GMT timezone), then I could use the above line of code to get UTC timestamp as I can directly add hours and minutes to the date
if a user enters a date of 13-Aug-2018 and time 8:45 (which is GMT +1, due to daylight savings time change) then with above line I might be creating a wrong date.
... I create a date object
let userDate = new Date(year, month, day)
Be careful here, you need to subtract 1 from the month, as they are numbered 0-11 by the Date instead of the usual 1-12.
and then from a user input where the user enters minutes and hours (into variables minutes and hours)
I need to convert this date and time into UTC timestamp, using moment ...
You don't need Moment for this. Just call .toISOString() on the Date object. It implicitly converts to UTC before generating the string.
var utcString = new Date(year, month-1, day, hours, minutes).toISOString();
... Can I just do
let utcTimestamp = moment.utc(date.toISOString()).hour(hours).minutes(minutes);
No, that doesn't make sense. That would only work if the date was entered in local time but the time was entered in UTC. Such a difference in behavior would surely be confusing to your user - especially if the UTC date was different than the local date.
... if a user enters a date of 13-Aug-2018 and time 8:45 (which is GMT +1, due to daylight savings time change) then with above line I might be creating a wrong date.
The conversion from local time to UTC will automatically take into account any daylight saving adjustment that is being observed by the local time zone. You do not need to do any additional adjustment on your own.
This is a snippet i used to convert an outlook calendar event from UTC to local time. The same Technique could be used for other scenarios.
//Get the users timezone
let timeZone = item.originalStartTimeZone;
//Get the start datetime in UTC time
let timeStart = item.start.dateTime;
//Calculate the negative offset.
let offset = - timeStart.localeCompare(timeZone);
//Add the calculated offset to the UTC start time
let localStartTime = addHours(timeStart, offset);

SQL DateTimes stored in UTC get beginning of day based on offset

This may be the most pathetic question ever asked related to SQL and date/time values, but I could use some help...
Trying to setup a function/job that will run at a specified time or times in eastern, mountain, central, and pacific time zones (in theory other zones would work too). The system will identify which users belong to each timezone and then output data from the system highlighting what they've accomplished for the current day.
Here is my challenge, I know all date/time values are stored on the SQL DB in UTC. I can apply the offset and convert those times to local time zones. Rather than convert tens of thousands of date/time values to local time and make comparisons there, it'd be cleaner (I think) to simply adjust the beginning and ending date/time values of UTC within the stored procedure.
On the west coast it is currently just about 2017-09-30 14:30:00 and in UTC is 2017-09-30 21:30:00, this clearly demonstrates a 7 hour time zone difference right now which means "today" from a user perspective technically started at 2017-09-30 07:00:00 and will end on 2017-10-01 06:59:999 in UTC.
What is the best way of establishing these date/time values for a users beginning of day and ending of day values?
UPDATES
I currently have this code...
DECLARE #InputDate as DateTime
DECLARE #InputEndDate as DateTime
DECLARE #InputDateWithOffset as DateTimeOffSet
DECLARE #InputEndDateWithOffset as DateTimeOffSet
SET #InputDate = '2017-09-28'
SET #InputDateWithOffset = #InputDate AT TIME ZONE 'UTC' AT TIME ZONE 'Pacific Standard Time'
SET #InputEndDate = DATEADD(day, 1, DATEADD(ms, -3, #InputDate))
SET #InputEndDateWithOffset = #InputEndDate AT TIME ZONE 'UTC' AT TIME ZONE 'Pacific Standard Time'
SELECT
#InputDate AS InputDate, #InputEndDate AS InputEndDate,
#InputDateWithOffset AS InputDateWithOffset,
#InputEndDateWithOffset AS InputEndDateWithOffset
Which outputs the following:
The last two columns appear to be correct as it would represent both the beginning of the Input Date and the ending of the Input Date as the Input Date is going to be the local date of the execution...
When I take the #InputDateWithOffset and #InputEndDateWithOffset against my table values with datetimes in UTC, it appears the only dates being returned are those that fall on 2017-09-28 and seems to disregard the comparisons to the Offset date/times.
Your update is mostly correct. However, you're missing a "start of day" operation, which needs to be done in local time.
Consider:
DECLARE #InputStartUTC as DATETIME, #InputEndUTC as DATETIME
DECLARE #InputStartDTO as DATETIMEOFFSET, #InputEndDTO as DATETIMEOFFSET
DECLARE #InputStartDTOatStartOfDay as DATETIMEOFFSET,
#InputEndDTOatStartOfDay as DATETIMEOFFSET
DECLARE #tz as VARCHAR(50) = 'Pacific Standard Time'
SET #InputStartUTC = '2017-09-28 00:00:00'
SET #InputStartDTO = #InputStartUTC AT TIME ZONE 'UTC' AT TIME ZONE #tz
SET #InputStartDTOatStartOfDay = CAST(CAST(#InputStartDTO as DATE) as DATETIME)
AT TIME ZONE #tz
SET #InputEndUTC = DATEADD(day, 1, #InputStartUTC)
SET #InputEndDTO = #InputEndUTC AT TIME ZONE 'UTC' AT TIME ZONE #tz
SET #InputEndDTOatStartOfDay = CAST(CAST(#InputEndDTO as DATE) as DATETIME)
AT TIME ZONE #tz
SELECT
#InputStartUTC as InputStartUTC, #InputEndUTC as InputEndUTC,
#InputStartDTO as InputStartDTO, #InputEndDTO as InputEndDTO,
#InputStartDTOatStartOfDay as InputStartDTOatStartOfDay,
#InputEndDTOatStartOfDay as InputEndDTOatStartOfDay
Also, notice I did not subtract three milliseconds from your end date. Rather than trying to figure out .997 or .999 or whatever, the better approach is to query using a half-open interval. In other words, start <= value AND end > value.

How to get timestamp of a remote country in VBA?

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)