zoukankan      html  css  js  c++  java
  • 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())

  • 相关阅读:
    postfix发信提示 Error: too many connectino from
    postfix 设置邮件头翻译,本域邮件不进行邮件头翻译,仅发送至外网的进行邮件头翻译?
    postfix 如何设置邮件头翻译的功能
    postfix 如何设置邮件头翻译的功能
    Oracle VM VirtualBox如何设置网络地址转换NAT
    python提取百度经验<标题,发布时间,平均流量,总流量,具体的链接>
    css 需要阴影的效果
    django POST表单的使用
    Vim中如何使用正则进行搜索
    Nginx 如何设置反向代理
  • 原文地址:https://www.cnblogs.com/webapi/p/5670685.html
Copyright © 2011-2022 走看看