How can I set TimeZone for my ASP.NET (MVC) Application - asp.net

My Application hosted at server in time zone which differs from mine. All date in database is not correct for my time zone.
How can I set my time zone for Application or how I can convert date to my time zone on output

Don't set the time zone for the process - use TimeZoneInfo from .NET 3.5 and higher to perform the relevant conversions.
Of course, that assumes you know the time zone that the data will come back in from the database... usually database records are kept in UTC, but not always...

There's very easy way to do it. Simply get the current UTC time and your timezone. Convert UTC to your timezone. Here's how you do it.
DateTime date1 = DateTime.UtcNow;
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("YOUR TIME ZONE (e.g. Pakistan Standard Time)");
DateTime date2 = TimeZoneInfo.ConvertTime(date1, tz);
Set your Time Zone in tz and then use "date2" anywhere.

Related

Converting a timestamp with a local timezone displayed in default BigQuery UTC format to actual UTC

I have an external data source automatically inserting data into my BigQuery table, this data source includes a timestamp field which does not have a timezone connected to it, however, I know this timestamp is in the Europe/Amsterdam timezone.
The problem here is that when this timestamp is inserted into BigQuery, BigQuery automatically defaults the timestamp to UTC, which it is not. And in my specific case, I want to convert this timestamp to UTC. However because BigQuery already defaulted the timestamp to UTC (while it is actually Europe/Amsterdam), I cannot easily convert it to the actual UTC timezone.
Is there any way to convert this timestamp, which BigQuery thinks is already UTC, to the actual UTC timezone within a query? I can't just give it a -02:00 offset due to Daylight Savings coming into play which changes this offset from 2 hours to only 1 hour depending on the time of year.
Any help would be appreciated, I have been kind of stuck on this :)
An example of the timestamp in BigQuery would be 2022-09-30 01:23:45 UTC
There is probably a better way but this should work
with
input as (select timestamp("2022-09-30 01:23:45 UTC") as ts)
select
ts,
timestamp(replace(cast(ts as string), '+00', " Europe/Amsterdam")) updated_ts
from input
ts
updated_ts
2022-09-30 01:23:45 UTC
2022-09-29 23:23:45 UTC

Moment isAfter day

So I need to check whether the current time is after a EndDate, which is a date stored as 19:00 UTC. I only need to know if it's the next day.
This is what I'm currently doing
const now = moment();
const tooLate = now.isAfter(moment(EndDate), 'day');
I worry about timezones. I supposedly need this for central time. Is this sufficient? Or do I need to worry about the timezone? As I understand it, moment runs in UTC to start with.
Depends on the format of EndDate. By default moment() will parse in local time, so unless EndDate is a JS Date object, a unix timestamp (e.g. 1318781876406), or in string form with UTC offset (e.g. 2013-02-08 09:30:26.123+07:00) then you will have a problem if it is not in the same timezone as the client.
You can use moment.UTC() to parse in UTC time instead of local time. This should be enough to solve the any formatting problems you might have with EndDate

Output DateTime as String with predefined time zone shift

I have a DateTime in my .NET program that I need to print with particular value of time zone offset (for instance, +01:00 always). Output should contain full date time with timezone. It has to be unrelated to system timezone setting. How I could achieve this?
Example: I have a timestamp such as 12-03-2016T12:30:34+03:00 and I need to output it calculated for predefined TZ +1: 12-03-2016T10:30:34+01:00
Found some approach to it.
First of all, DateTime does not have time zone stored in it. Instead it has flag whether it is UTC or Local (without the idea what Local TZ shift is). So: first thing is to get your initial parsing of time from any string time stamp in UTC.
Once it is stored in DateTime object (with Kind=UTC), you have to convert it to the timezone you desire output for. I find examples here useful: datetime to string with time zone.
Note: if you need to convert London daylight-saving time, you have to know right names of timezones in NET so you get it right. See Difference between UTC and GMT Standard Time in .NET

DateTime Json Return From WebAPI with Default Serializer

I know this question has been hashed over multiple times and I read lots of posts on that hashing but still am confused.
Using MVC4/WebAPI, I have a datetime that is simply created as new DateTime.Now.
My WebAPI is return data like this:
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new
{
data = sessionRecordSmalls,
count = sessionRecordSmalls.Count,
success = true
});
where sessionRecordsSmall has a public property of DateTime in it.
When I look at the date in the VS debugger, it shows it as now with no timezone of course because DateTime does not include a timezone.
{10/6/2012 9:45:00 AM}
When I look at what gets downloaded from the server, I see in the JSON
2012-10-06T09:45:00
I think the T0 means Timezone 0, not 100% sure of that. My JavaScript library interprets it as timezone 0, then shows the actual date downloaded as GMT (-9 hours ago for me).
My question is, what is the JSON downloaded? Is that include a timezone? Am I missing some important step here?
if serializing with json.net keep in mind that you can specify DateTimeZoneHandling.
Example in WebApiConf.cs
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.DateTimeZoneHandling =Newtonsoft.Json.DateTimeZoneHandling.Local;
The date time 2012-10-06T09:45:00, which we recive in JSON with Web API and default serializer is the ISO 8601 format.
In fact this is so called Combined date and time representations. Extract:
..A single point in time can be represented by concatenating a
complete date expression, the letter T as a delimiter, and a valid
time expression. For example "2007-04-05T14:30"...
There is no time zone information in this format. As mentioned in the Time zone designators Extract:
Time zones in ISO 8601 are represented as local time (with the
location unspecified), as UTC, or as an offset from UTC. If no UTC
relation information is given with a time representation, the time is
assumed to be in local time.
In other words, if there is no offset from UTC specified, it is treated as a local time.
The UTC format would be extended with Z at the end
If the time is in UTC, add a Z directly after the time without a
space. Z is the zone designator for the zero UTC offset. "09:30 UTC"
is therefore represented as "09:30Z" or "0930Z". "14:45:15 UTC" would
be "14:45:15Z" or "144515Z".
UTC time is also known as 'Zulu' time, since 'Zulu' is the NATO phonetic alphabet word for 'Z'.
So, the date-time we recieve is the ISO 8601 format, treated as local time zone (no Z at the end like this 2012-10-06T09:45:00Z)

Working with Time Zones in SSRS

We store all our dates SQL Server 2008 database in UTC time in DateTime columns.
I'm using SSRS to create reports and I need to convert all the times on the reports to the Time Zone of the computer where they're running the report from.
I know could always just pass in the current timezone offset as a parameter to the report and add or subtract the offset from the timezone, but this wouldn't correctly show historical dates because of daylight savings.
Does SSRS have any functions that handle this? Should I pass the timezone to the SQL server functions and have the SQL Server convert the time?
I figured it out.
In the SSRS report, I've added a reference to the assembly System.Core
Then anywhere I needed to convert the timezone I used:
=Code.FromUTC(Fields!UTCDateFromDatabase.Value, Parameters!TimeZone.Value)
where Parameters!TimeZone.Value is the string value of the timezone which can be retrieved in the application by using TimeZone.CurrentTimeZone.StandardName
I should probably put what FromUTC does:
Shared Function FromUTC(ByVal d As Date, ByVal tz As String) As Date
Return (TimeZoneInfo.ConvertTimeBySystemTimeZoneId(d, TimeZoneInfo.Utc.Id, tz))
end function
Try using this instead:
=System.TimeZone.CurrentTimeZone.ToLocalTime(Fields!DateTime.Value)
For SSRS reports called from AX 2012, this is what I use.
Credit goes to this post: http://social.msdn.microsoft.com/Forums/en-US/500448a3-bf58-44ab-8572-81becd67d8b8/convert-utc-time-to-local-time?forum=sqlreportingservices

Resources