js时间戳和c#时间戳互转方法(推荐)
实例如下:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Web; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; usingNewtonsoft.Json; namespaceTestWeb { publicpartialclassajax:System.Web.UI.Page { protectedvoidPage_Load(objectsender,EventArgse) { if(!IsPostBack) { //TestAjax() } } publicvoidTestAjax() { varphone=Request.Form["phone"]; varauthcode=Request.Form["authcode"]; varpt=Request.Form["pt"];//js时间戳newDate().getTime()eg:1429503106452 stringoutputmsg=string.Empty; if(phone!=null&&authcode!=null&&pt!=null) { DateTimedtStart=TimeZone.CurrentTimeZone.ToLocalTime(newDateTime(1970,1,1)); //说明下,时间格式为13位后面补加4个"0",如果时间格式为10位则后面补加7个"0" longlTime=long.Parse(pt+(pt.Length==13?"0000":"0000000")); TimeSpantoNow=newTimeSpan(lTime); DateTimedtResult=dtStart.Add(toNow);//得到转换后的时间 stringstr=dtResult.ToString(); outputmsg=OutMsg(newResponseInfo{success=true,tag=100,msg="成功"}); } Response.Write(outputmsg); } publiclongGetCurrentTicksForJs() { System.DateTimestartTime=TimeZone.CurrentTimeZone.ToLocalTime(newSystem.DateTime(1970,1,1,0,0,0,0)); DateTimedtResult=DateTime.Now;//获取时间 longt=(dtResult.Ticks-startTime.Ticks)/10000;//除10000调整为13位 returnt; } publicstringOutMsg(objectobj) { returnJsonConvert.SerializeObject(obj,Newtonsoft.Json.Formatting.Indented); } publicclassResponseInfo { publicboolsuccess{get;set;} publicinttag{get;set;} publicstringmsg{get;set;} } //... } }<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="ajax.aspx.cs"Inherits="TestWeb.ajax"%> <scripttype="text/javascript"> vard=newDate(<%=GetCurrentTicksForJs()%>); alert(formatDate(d)); functionformatDate(now){ varyear=now.getFullYear(); varmonth=now.getMonth()+1; vardate=now.getDate(); varhour=now.getHours(); varminute=now.getMinutes(); varsecond=now.getSeconds(); returnyear +"-" +(month.toString().length==1?"0"+month:month) +"-" +(date.toString().length==1?"0"+date:date)+""+hour+":"+minute+":"+second; } </script> vardate=newDate(1459481266695); Y=date.getFullYear()+'-'; M=(date.getMonth()+1<10?'0'+(date.getMonth()+1):date.getMonth()+1)+'-'; D=date.getDate()+''; h=date.getHours()+':'; m=date.getMinutes()+':'; s=date.getSeconds(); console.log(Y+M+D+h+m+s); VM307:92016-04-111:27:46
以上这篇js时间戳和c#时间戳互转方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。