Asp.Net Unix時間戳和DateTime類型轉換


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class weiapi_ceshi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(ConvertIntDateTime(1468482273277));
    }
    /// <summary>
    /// 將Unix時間戳轉換為DateTime類型時間
    /// </summary>
    /// <param name="d">double 型數字</param>
    /// <returns>DateTime</returns>
    public static System.DateTime ConvertIntDateTime(double d)
    {
        System.DateTime time = System.DateTime.MinValue;
        System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
        time = startTime.AddMilliseconds(d);
        return time;
    }

    /// <summary>
    /// 將c# DateTime時間格式轉換為Unix時間戳格式,返回格式:1468482273277
    /// </summary>
    /// <param name="time">時間</param>
    /// <returns>long</returns>
    public static long ConvertDateTimeInt(System.DateTime time)
    {
        //double intResult = 0;
        System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
        //intResult = (time- startTime).TotalMilliseconds;
        long t = (time.Ticks - startTime.Ticks) / 10000;            //除10000調整為13位
        return t;
    }
}

其他獲取當前時間戳

Java

time

JavaScript

Math.round(new Date() / 1000)

.NET / C#

(DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000

PHP

time()

MySQL

SELECT unix_timestamp(now())

SQL Server

SELECT DATEDIFF(s, '1970-01-01 00:00:00', GETUTCDATE())

SQLite

SELECT strftime('%s', 'now')

PostgreSQL

SELECT extract(epoch FROM now())

Python

先 import time 然后 time.time()

Ruby

獲取Unix時間戳:Time.now 或 Time.new顯示Unix時間戳:Time.now.to_i

Perl

time

Swift

NSDate().timeIntervalSince1970

Objective-C

[[NSDate date] timeIntervalSince1970]

Erlang

calendar:datetime_to_gregorian_seconds(calendar:universal_time())-719528*24*3600.

Go

import ("time") int32(time.Now().Unix())

Unix / Linux

date +%s

VBScript / ASP

DateDiff("s", "01/01/1970 00:00:00", Now())


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM