Home >Web Front-end >JS Tutorial >Timestamp conversion between js and C#

Timestamp conversion between js and C#

PHPz
PHPzOriginal
2016-05-16 16:31:301804browse

Convert JS timestamp to time in C#, and then convert the timestamp in C# to JS time

Timestamp in JS

var dt = new Date().getTime();//时间戳

C# timestamp to time

DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime( new DateTime(1970, 1, 1));
long lTime = long.Parse(dt + "0000");  //说明下,时间格式为13位后面补加4个"0",如果时间格式为10位则后面补加7个"0",至于为什么我也不太清楚,也是仿照人家写的代码转换的
TimeSpan toNow = new TimeSpan(lTime);
DateTime dtResult = dtStart.Add(toNow); //得到转换后的时间

C# time to timestamp

System. DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime( new System. DateTime(1970, 1, 1, 0, 0, 0, 0));//intResult = (time- startTime).TotalMilliseconds;
DateTime dtResult//获取时间         
long t = (dtResult.Ticks - startTime.Ticks) / 10000;//除10000调整为13位

JS

var d = new Date(data);  //时间戳转时间
alert(formatDate(d)); 
//格式化时间
function formatDate(now){    
   var year=now.getFullYear();    
   var month=now.getMonth()+1;    
   var date=now.getDate();    
   var hour=now.getHours();    
   var minute=now.getMinutes();    
   var second=now.getSeconds();    
   return year+ "-" + month + "-"+ date + "" + hour + ":" +minute+":" + second;    
    }

The code is super simple, but very practical, friends, please refer to it; for more related tutorials, please visit JavaScript video tutorial, C# video tutorial!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn