UTC時間と北京時間が入れ替わる
2199 ワード
第一の方法
タイムスタンプとは、グリニッジ時間1970年01月01日00時00分00秒(北京時間1970年01月01日08時00分00秒)から現在までの総秒数のこと.public class HomeController : Controller
{
public ActionResult Index()
{
// ( )
DateTime dt = Convert.ToDateTime("2016-06-11 23:59:59");
//TimeZone ,TimeZone.CurrentTimeZone : 。
TimeZone tz = TimeZone.CurrentTimeZone;
// utc ( utc 8 )
TimeSpan ts = tz.GetUtcOffset(dt);
// ( : ) UTC
DateTime dtGMT = tz.ToUniversalTime(dt);
// UTC
DateTime localtime = tz.ToLocalTime(dtGMT);
return View();
}
}
第2の方法 public class HomeController : Controller
{
public ActionResult Index()
{
// ( )
DateTime dt = Convert.ToDateTime("2016-06-11 23:59:59");
// utc ( utc +8 , utc 2016-06-11 15:59:59)
DateTime utcNow = dt.ToUniversalTime();
// utc ( 1970-01-01 00:00:00 2016-06-11 15:59:59 )
double utc = ConvertDateTimeInt(utcNow);
// ( utc ), 2016-06-11 23:59:59
DateTime dtime = ConvertIntDatetime(utc);
return View();
}
///
/// ( 1970-1-1 00:00:00 )
///
///
///
public double ConvertDateTimeInt(System.DateTime time)
{
double intResult = 0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (time - startTime).TotalSeconds;
return intResult;
}
///
///
///
///
///
public DateTime ConvertIntDatetime(double utc)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
startTime = startTime.AddSeconds(utc);
startTime = startTime.AddHours(8);// ( =UTC +8 )
return startTime;
}
}
public class HomeController : Controller
{
public ActionResult Index()
{
// ( )
DateTime dt = Convert.ToDateTime("2016-06-11 23:59:59");
//TimeZone ,TimeZone.CurrentTimeZone : 。
TimeZone tz = TimeZone.CurrentTimeZone;
// utc ( utc 8 )
TimeSpan ts = tz.GetUtcOffset(dt);
// ( : ) UTC
DateTime dtGMT = tz.ToUniversalTime(dt);
// UTC
DateTime localtime = tz.ToLocalTime(dtGMT);
return View();
}
}
public class HomeController : Controller
{
public ActionResult Index()
{
// ( )
DateTime dt = Convert.ToDateTime("2016-06-11 23:59:59");
// utc ( utc +8 , utc 2016-06-11 15:59:59)
DateTime utcNow = dt.ToUniversalTime();
// utc ( 1970-01-01 00:00:00 2016-06-11 15:59:59 )
double utc = ConvertDateTimeInt(utcNow);
// ( utc ), 2016-06-11 23:59:59
DateTime dtime = ConvertIntDatetime(utc);
return View();
}
///
/// ( 1970-1-1 00:00:00 )
///
///
///
public double ConvertDateTimeInt(System.DateTime time)
{
double intResult = 0;
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
intResult = (time - startTime).TotalSeconds;
return intResult;
}
///
///
///
///
///
public DateTime ConvertIntDatetime(double utc)
{
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
startTime = startTime.AddSeconds(utc);
startTime = startTime.AddHours(8);// ( =UTC +8 )
return startTime;
}
}