欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js时间戳和c#时间戳互转方法(推荐)

程序员文章站 2023-11-25 17:32:16
实例如下: using system; using system.collections.generic; using system.linq; using...

实例如下:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using newtonsoft.json;

namespace testweb
{
  public partial class ajax : system.web.ui.page
  {
    protected void page_load(object sender, eventargs e)
    {
      if (!ispostback)
      {
        //testajax()
      }
    }

    public void testajax()
    {
      var phone = request.form["phone"];
      var authcode = request.form["authcode"];
      var pt = request.form["pt"]; //js时间戳 new date().gettime() eg: 1429503106452

      string outputmsg = string.empty;

      if (phone != null && authcode != null && pt != null)
      {
        datetime dtstart = timezone.currenttimezone.tolocaltime(new datetime(1970, 1, 1));
        //说明下,时间格式为13位后面补加4个"0",如果时间格式为10位则后面补加7个"0"
        long ltime = long.parse(pt + (pt.length == 13 ? "0000" : "0000000"));
        timespan tonow = new timespan(ltime);
        datetime dtresult = dtstart.add(tonow); //得到转换后的时间

        string str = dtresult.tostring();
        outputmsg = outmsg(new responseinfo { success = true, tag = 100, msg = "成功" });
      }

      response.write(outputmsg);
    }

    public long getcurrentticksforjs()
    {
      system.datetime starttime = timezone.currenttimezone.tolocaltime(new system.datetime(1970, 1, 1, 0, 0, 0, 0));
      datetime dtresult = datetime.now;//获取时间     
      long t = (dtresult.ticks - starttime.ticks) / 10000;//除10000调整为13位
      return t;
    }

    public string outmsg(object obj)
    {
      return jsonconvert.serializeobject(obj, newtonsoft.json.formatting.indented);
    }

    public class responseinfo
    {
      public bool success { get; set; }
      public int tag { get; set; }
      public string msg { get; set; }
    }

    //...

  }
}<%@ page language="c#" autoeventwireup="true" codebehind="ajax.aspx.cs" inherits="testweb.ajax" %>

<script type="text/javascript">
  var d = new date(<%=getcurrentticksforjs() %>);
  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.tostring().length ==1 ? "0"+month : month) 
        + "-" 
        + (date.tostring().length ==1 ? "0"+date : date) + " " + hour + ":" + minute + ":" + second;
  }
</script>
var date = new date(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:9 2016-04-1 11:27:46

以上这篇js时间戳和c#时间戳互转方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。