Utc时间跟Local时间的区别


UTC即世界标准时间,北京时间与UTC的时差为+8,也就是UTC+8=北京时间

一般保存在数据库里是存UTC时间,然后在页面再转为LOCAL时间。

JS把UTC时间转为LOCAL时间的方法如下:

   var localDate = new Date(utcDate.toString());

LOCAL时间跟UTC时间的区别代码测试如下:

后端代码:

using System;
using System.Web.Mvc;

namespace CloudCodeTest.Controllers
{
    public class TimeTestController : Controller
    {
        // GET: TimeTest
        public ActionResult Index()
        {
            DateTime utcTime = DateTime.UtcNow;
            DateTime localTime = DateTime.Now;
            // return View(utcTime);//好像不能直接这么写,需要建立一个Model,如下:
            var model = new DateTimeTestModel
            {
                utcTime = utcTime,
                localTime = localTime
            };
            return View(model);

        }
    }

    public class DateTimeTestModel
    {
        public DateTime utcTime { get; set; }
        public DateTime localTime { get; set; }
    }
}

前端代码:

@{
    ViewBag.Title = "Index";
}

Index

UTC 时间为: @Model.utcTime
本地时间为:@Model.localTime

测试结果如下:

 代码目录:C...CodeTest=>TimeTestController

C