0

私は文字列を持っていると言います"1 December 2012, 8:00:00"

(サーバーがどこにあるかに関係なく)AEST時間であり、その時点(12月1日)の夏時間はどのようなものでもかまいませんが、UTCとしてデータベースに保存したいと思います。

サーバーの場所に関係なく文字列をAESTとして扱い、UTCに変換するにはどうすればよいですか?

4

3 に答える 3

3

で日付を解析DateTime.TryParseして構造体に組み込むことができると仮定すると、UTCに変換するためにDateTime呼び出すことができます。ToUniversalTime

例えば:

DateTime dt;
DateTime utcDate;
if (DateTime.TryParse(dateTimeString, out dt))
{
    utcDate = dt.ToUniversalTime();
    // store utcDate in database
}
else
{
    // error, unable to parse the date
}

もちろん、現地時間がAESTであるか、解析している日付/時刻文字列にタイムゾーン指定子があると仮定します。

現地時間が別のものであり、タイムゾーン指定子がない場合は、日付を解析し、タイムゾーンからオフセットを加算または減算してAESTと一致させてから、を呼び出す必要がありますToUniversalTime

于 2012-12-07T00:40:29.640 に答える
2

Picrofo EGY、esr、White Dragonのおかげで、私はそれを解決するこの関数を作成しました

public static DateTimeOffset CreateDateWithTimezone(string dateStr, TimeZoneInfo tzi)
{
  DateTimeOffset dtoTzi = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, tzi);

  DateTimeOffset dto;
  if (!DateTimeOffset.TryParse(dateStr + " " + dtoTzi.ToString("zzz"), out dto))
    throw new Exception("Failed to parse date: " + dateStr + " " + dtoTzi.ToString("zzz"));

  if (tzi.SupportsDaylightSavingTime)
  {
    TimeSpan offset = tzi.GetUtcOffset(dto);
    string offsetStr = (offset.TotalHours < 0 ? "" : "+") + offset.Hours.ToString("00") + ":" + offset.Minutes.ToString("00");

    if (!DateTimeOffset.TryParse(dateStr + " " + offsetStr, out dto))
      throw new Exception("Failed to parse date: " + dateStr + " " + dtoTzi.ToString("zzz"));
  }

  return dto;
}

使用法:

string dateStr = "1 December 2012, 08:00:00";
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");
DateTimeOffset dtoUtc = CreateDateWithTimezone(dateStr, tzi).ToUniversalTime();

Response.Write("dtoUtc is " + dtoUtc);

印刷されます

dtoUtcは2012年11月30日午後9時00分00秒+00:00です

完全!

于 2012-12-07T01:20:46.177 に答える
2

System.DateTime最初に、上記で指定した文字列からを取得するを初期化DateTimeできます。次に、これを使用System.DateTime.ToUniversalTime()して、現在のオブジェクトの値を協定世界時に変換System.DateTimeし、データベースに保存できます。

DateTime AEST = DateTime.Parse("1 December 2012, 8:00:00"); //Initialize a new DateTime of name AEST which gets a System.DateTime from 1 December 2012, 8:00:00
DateTime UTC = AEST.ToUniversalTime(); //Initialize a new DateTime of name UTC which converts the value from AEST to Coordinated Universal Time
System.Diagnostics.Debug.WriteLine(UTC.ToString()); //Writes 12/1/2012 6:00:00 AM

注意:あるタイムゾーンから別のタイムゾーンに時間を変換する場合TimeZoneInfo.ConvertTime(DateTime dateTime, TimeZoneInfo sourceTimeZone, TimeZoneInfo, destinationTimeZone)dateTime、変換する場所DateTime、変換するsourceTimeZone時間帯、DateTimeおよびdestinationTimeZone取得する時間帯を使用できます。dateTime

string _date = "1 December 2012, 8:00:00"; //Initializes a new string of name _date as "1 December 2012, 8:00:00"
string targetTimeZone = "W. Australia Standard Time"; //Initializes a new string of name targetTimeZone as "W. Australia Standard Time"
DateTime sourceDateTime = DateTime.Parse(_date); //Initializes a new DateTime of name sourceDateTime which gets a valid DateTIme object from 1 December 2012, 8:00:00
DateTime AEST = TimeZoneInfo.ConvertTime(sourceDateTime, TimeZoneInfo.Local, TimeZoneInfo.FindSystemTimeZoneById(targetTimeZone)); //Initializes a new DateTime which converts the time zone from sourceDateTime assuming that sourceDateTime's TimeZone is the local time zone of the machine to an W. Australia Standard Time time zone of name AEST  

1 December 2012, 8:00:00これは、そのタイムゾーンがマシンのローカルタイムゾーン(たとえばEgypt Standard Time: (GMT+02:00) Cairo)であると想定して変換W. Australia Standard Time: (GMT+08:00) Perthされます12/1/2012 2:00:00 PM

タイムゾーンのリストについては、Microsoftのタイムゾーンインデックス値を参照してください。

ありがとう、
これがお役に立てば幸いです:)

于 2012-12-07T00:39:46.077 に答える