Utc时间跟Local时间的区别
utctime=localTime+localOffset; timeOffset is in minutes
注意在C#中,ToLocalTime()?是不是根据服务器的时间,真正要换成客户的时间,只能用timezoneoffset吗?
客户端timezoneoffset的获取方法是: var offSet = new Date().getTimezoneOffset();
把这个传到服务器里,用utctime-offSet就是客户端的时间,注意offSet是根据分钟数的。
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"; } <h2>Index</h2> UTC 时间为: @Model.utcTime <br /> 本地时间为:@Model.localTime
测试结果如下:
代码目录:C...CodeTest=>TimeTestController
本文来自博客园,转载请注明原文链接:https://www.cnblogs.com/keeplearningandsharing/p/15883983.html
分类:
C#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通