>  기사  >  백엔드 개발  >  C# 타임스탬프와 js 타임스탬프의 상호 변환 방법을 자세히 소개하는 코드 공유

C# 타임스탬프와 js 타임스탬프의 상호 변환 방법을 자세히 소개하는 코드 공유

黄舟
黄舟원래의
2017-03-23 11:28:562018검색

아래 편집기에서는 js 타임스탬프와 c# 타임스탬프(권장)를 변환하는 방법에 대한 기사를 제공합니다. 에디터가 꽤 좋다고 생각해서 지금 공유해서 참고용으로 올려보겠습니다. 편집자를 따라가서 살펴보겠습니다

예제는 다음과 같습니다.

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() + &#39;-&#39;;
M = (date.getMonth()+1 < 10 ? &#39;0&#39;+(date.getMonth()+1) : date.getMonth()+1) + &#39;-&#39;;
D = date.getDate() + &#39; &#39;;
h = date.getHours() + &#39;:&#39;;
m = date.getMinutes() + &#39;:&#39;;
s = date.getSeconds(); 
console.log(Y+M+D+h+m+s); 
VM307:9 2016-04-1 11:27:46

위 내용은 C# 타임스탬프와 js 타임스탬프의 상호 변환 방법을 자세히 소개하는 코드 공유의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.