English 中文(简体)
时间段
原标题:datetime to string with time zone

我有一个时间储存在普遍时间(UTC),价值2010-01 01:01:01

我愿以这种格式以2010-01 04:01:01GMT-04:00表示,然而,时间区所用的K格式在《为确定时间区的工作》中并不奏效。

最佳回答

采用“zzz”格式的投机者获得“UTC”的抵消。 例如:

        var dt = new DateTime(2010, 1, 1, 1, 1, 1, DateTimeKind.Utc);
        string s = dt.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss "GMT"zzz");
        Console.WriteLine(s);

Output: 2009-12-31 19:01:01 GMT-06:00

I m in the CDT timezone. 确保日期明确无误。 北方。

问题回答

如同我一样,你需要一种格式,如<条码>2018-03-31T01:23:45.678-0300<>>。 (时区没有殖民地),你可以这样做:

datetime.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz").Remove(26,1)

这种方法将回到东部标准时间的指定时间(如问题要求), 甚至如果无害环境技术不是当地时间区<>:

public string GetTimeInEasternStandardTime(DateTime time)
{
    TimeZoneInfo easternStandardTime = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    DateTimeOffset timeInEST = TimeZoneInfo.ConvertTime(time, easternStandardTime);
    return timeInEST.ToString("yyyy-MM-dd hh:mm:ss tt" GMT"zzz");
}

注:我没有在非英语的非洲顾问处测试。 见。 TimeZoneInfo.FindSystemtimeZoneById

与这项工作类似。 也许可以把它清理起来更远:

string newDate = string.Format("{0:yyyy-MM-dd HH:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"));

I think you are looking for the TimeZoneInfo class (see http://msdn.microsoft.com/en-us/library/system.timezoneinfo_members.aspx). It has many static methods to convert dates between time zones.





相关问题
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. ...