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())

posted @ 2016-07-14 15:49  WebApi  阅读(5200)  评论(0编辑  收藏  举报
CopyRight © 博客园 WebAPI