VB.NET - Find current timezone, then convert to Pacific - vb.net

I tried searching around for the code, but I didn't have too much luck. I found little snippets and I know how to convert current time to UTC, but I just really want to know how to get the current time zone, then convert the time to pacific.
I have many users that use my software all the way from california to maine. So I need to find out what timezone they are in, then convert it to pacific no matter where they are so I can accurately compare the local time to my servers time.

You can use the TimeZoneInfo class to convert time zones for you:
Dim now As DateTime = DateTime.Now
MessageBox.Show(now.ToString())
Dim pacificNow = System.TimeZoneInfo.ConvertTime(now, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"))
MessageBox.Show(pacificNow.ToString())
You can read more about the TimeZones and the Ids on MSDN.

Related

Is there a SQL function to convert to PST or PDT depending on the date?

I would like to convert times from pacific to UTC. However, I must first convert the times to either PST or PDT depending on the date. Is there a SQL function that can do this, or does anyone have any advice for creating this function?
The link you provided is pretty much useless as a guide to timestamps. If you are going to work with timezones then store your timestamps in a timestamp with time zone field. The timezone will not actually be stored but the timestamp will be stored as a UTC. Whenever a timestamp is entered it is rotated to a UTC value. This makes working with value easier down the road. If you want to take into account DST transitions you will need to use full timezone names e.g. US/Pacific, as they cover the two offsets(PST/PDT) that constitute the standard/daylight savings timezones. As you found using the offset PST(-08) or PDT(-07) gets you a fixed offset regardless of date.

How do you return a timestamp from setTimezone function?

I want to be able to convert the current UTC time to a user's timezone, and get the value as a timestamp. Simple.
I seem to be having some difficulty though... this is what I've tried:
Carbon::now()->setTimezone('America/Los_Angeles'); // this works, but returns the time in the format "Y-m-d H:i:s"
So then I tried this:
Carbon::now()->setTimezone('America/Los_Angeles')->timestamp; // for some reason doing this seems to revert the timezone conversion and instead returns the timestamp for the current UTC time? :/
So, I'm using this workaround:
$converted_time = Carbon::now()->setTimezone('America/Los_Angeles');
return Carbon::createFromFormat('Y-m-d H:i:s', $converted_time)->timestamp;
...which gives me what I need, but seems unnecessary. Is there a better way to do this (in one line)?
Thanks.
The Unix Timestamp is per definition the number of second since 1st January 1970 UTC.
If you're trying to calculate the number of second since an other date (like 1st January in Los Angeles), you're calculating something else that is not Unix Timestamp.
To make sense, it's very important that the timestamp as a number would be a non-ambigus representation of a single second that can be used safely worldwide and still represent the same moment. If you have a number that can be different moment regarding from which timezone you're looking at it, you're likely not using the right tool, and would get more safety to store date-time + timezone.
So the very first question, is why would you need to get the number of second since 1st January in Los Angeles?
You can read more here about how to properly work with UTC and why to avoid having a strong dependence in your back-end with calculations in other timezones:
https://kylekatarnls.medium.com/always-use-utc-dates-and-times-8a8200ca3164

Find current UK time

I'm writing a system which will be used in the US, however it must make reference to the current UK time.
I need an offline solution which will display the current UK time (taking into account GMT and BST)
Is there an inbuilt function in vb.NET which can handle this, or is there a programatic way of retrieving this value?
Dim dt as DateTime
dt = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "GMT Standard Time")
This gets the current time in UTC, and converts it to the time in the United Kingdom. Do not be confused by the wording of the time zone identifier, it indeed covers both GMT and BST, and the transitions between them.

Time difference issue in two different countries

There are two clients with same window application. One is in India and other one is in Belgium. Sql server and web service application is hosted at Belgium. In sql I am storing UTC date time.
Now issue is a time difference for this two clients. I want to show UTC time in history form that mean what is stored in Database I have to bind that data to gird. No any extra code because I suppose to bind UTC date-time. Event then I get time difference for this two client.
Blue header screen is of a Indian client and other one is of a Belgium client. In Belgium time is showing exactly as in Database but difference is for India. Am I missing anything in configuration or what?
You have to convert both time zone in standard UTC and save it. Used dateadd() function to manipulate datetime. such as
declare #IST_date datetime
declare #BE_date datetime
declare #UTC_date datetime
--Indian standard time is (GMT + 5:30 hrs)
-- Belgium standard time is ( GMT + 1 hour)
select #UTC_date = DATEADD(hh,5.30, #IST_date)
select #UTC_date = DATEADD(hh,1, #BE_date)
why not simply convert the value at display ?
DateTime MyDate = Data["ChangedDate"];
DateTime MyDateUTC = MyDate.ToUniversalTime();
tadaaaaa
Ditch System.DateTime and use Noda Time!
Getting Started with Noda Time
System.DateTime uses the system culture and time zone at unpredictable moments, where Noda Time works without any defaults. Takes a bit of work to understand, but you'll never look back.
Use Noda time on the client, and store all the values in UTC in the database. You may also wish to store the original time zone that the date time was entered in.

Convert From Local Time to UTC (Given the local TimeZone)

I've dates in different time zones.
How to convert them to UTC giving the respective time zone.
Something like this:
Dim Dated as DateTime = TempDate.ConvertToUniversalTime(TimeZone)
The DateTime object in .net provides the DateTime.ToUniversalTime method. Since you are using the VB Date type the DateTime.FromOADate and DateTime.ToOAdate will provide conversion between the Date and DateTime types (see Interop Considerations under Programming Tips here).
UPDATE: You might also want to check out Converting Times Between Time Zones.
Here's how to do it
Dim TimeZone As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time")
Dim Dated As DateTime = TimeZoneInfo.ConvertTimeToUtc(TempDate, TimeZone)
For the TimeZones IDs check:
TimeZone.GetSystemTimeZones