English 中文(简体)
C# 日期.Now.ToString (EST)
原标题:C# DateTime.Now.ToString (EST)
  • 时间:2023-10-04 23:53:48
  •  标签:
  • c#
  • datetime

My App is connected to Database and it captures the time which is displayed within application. All records have time of creation and date/time should be in EST because our users are working in different timezones.

因此,我的问题是,如何显示时间。 如今,在无害环境技术中,则使用系统或超时技术。

现行法典:

   private void timer1_Tick(object sender, EventArgs e)
        {
            label2date_.Text = DateTime.Now.ToString("MM dd yy HH:mm");
         
        }
问题回答

<>Explanation>

您可使用timeZoneInfo模块获取当前当地时间区的信息,并使用。 TimeZoneInfo.Converttime do the transformation from one timezone to another.

<><><>>>>

private void timer1_Tick(object sender, EventArgs e)
    {
    
        DateTime dtNow = DateTime.Now.ToLocalTime();
        // Define the time zone for your local time
        TimeZoneInfo localtz = TimeZoneInfo.Local;
        // Define the Eastern Standard Time (EST) zone
        TimeZoneInfo esttz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
        // Convert local time to EST
        DateTime dtEST = TimeZoneInfo.ConvertTime(dtNow, localtz, esttz);
    
        Console.WriteLine("Local Time: " + dtNow.ToString("MM dd yy HH:mm"));
        Console.WriteLine("EST Time: " + dtEST.ToString("MM dd yy HH:mm"));
    
        label2date_.Text = dtEST.ToString("MM dd yy HH:mm");
    }

如果你想在网站上向用户展示当地时间,并能够支持多个时区<>,那么最好在后端代码中使用点时序,使用户代码(~javascript)能够翻译。


MS Learn has a section for translating time in the [Converting times between time zones](https://learn.microsoft.com/en-us/dotnet/standard/datetime/converting-between- time-zones#converting-utc-to-a-designated-time-zone) article.

例如:

DateTime utcNow = DateTime.UtcNow; // UTC, not local machine time

var tz = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time"); // or Eastern etc.
DateTime cstNow = TimeZoneInfo.ConvertTimeFromUtc(utcNow, tz);

Console.WriteLine($"Utc: {utcNow}");
Console.WriteLine($"CST: {cstNow}");

produces:

Utc: 5/10/2023 2:49:19 AM
CST: 4/10/2023 9:49:19 PM

同样,请指出,如果用户使用多个时区,这不会奏效。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...