zoukankan      html  css  js  c++  java
  • 时间与时间戳之间的转换

    function pad0(i) {
    return (i < 10 ? '0' + (i) : i)
    };

    function f(timestamp) {
    const date = new Date(timestamp * 1000);
    const Y = date.getFullYear() + '-';
    const t = pad0;
    const M = t(date.getMonth() + 1) + '-';
    const D = t(date.getDate()) + ' ';
    const h = t(date.getHours()) + ':';
    const m = t(date.getMinutes()) + ':';
    const s = t(date.getSeconds());

    return Y + M + D + h + m + s;
    }

    python——时间与时间戳之间的转换 - CSDN博客 https://blog.csdn.net/google19890102/article/details/51355282

    对于时间数据,如2016-05-05 20:28:54,有时需要与时间戳进行相互的运算,此时就需要对两种形式进行转换,在Python中,转换时需要用到time模块,具体的操作有如下的几种:

    将时间转换为时间戳
    重新格式化时间
    时间戳转换为时间
    获取当前时间及将其转换成时间戳
    1、将时间转换成时间戳

    将如上的时间2016-05-05 20:28:54转换成时间戳,具体的操作过程为:

    利用strptime()函数将时间转换成时间数组
    利用mktime()函数将时间数组转换成时间戳
    #coding:UTF-8
    import time

    dt = "2016-05-05 20:28:54"

    #转换成时间数组
    timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
    #转换成时间戳
    timestamp = time.mktime(timeArray)

    print timestamp

    2、重新格式化时间

    重新格式化时间需要以下的两个步骤:

    利用strptime()函数将时间转换成时间数组
    利用strftime()函数重新格式化时间
    #coding:UTF-8
    import time

    dt = "2016-05-05 20:28:54"

    #转换成时间数组
    timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
    #转换成新的时间格式(20160505-20:28:54)
    dt_new = time.strftime("%Y%m%d-%H:%M:%S",timeArray)

    print dt_new

    3、将时间戳转换成时间

    在时间戳转换成时间中,首先需要将时间戳转换成localtime,再转换成时间的具体格式:

    利用localtime()函数将时间戳转化成localtime的格式
    利用strftime()函数重新格式化时间
    #coding:UTF-8
    import time

    timestamp = 1462451334

    #转换成localtime
    time_local = time.localtime(timestamp)
    #转换成新的时间格式(2016-05-05 20:28:54)
    dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)

    print dt

    4、按指定的格式获取当前时间

    利用time()获取当前时间,再利用localtime()函数转换为localtime,最后利用strftime()函数重新格式化时间。

    #coding:UTF-8
    import time

    #获取当前时间
    time_now = int(time.time())
    #转换成localtime
    time_local = time.localtime(time_now)
    #转换成新的时间格式(2016-05-09 18:59:20)
    dt = time.strftime("%Y-%m-%d %H:%M:%S",time_local)

    print dt

  • 相关阅读:
    支付宝及时到帐接口使用详解
    简便无刷新文件上传系统
    EyesBaby功能实现之窗口拖拽与缩放功能
    Jquery各行换色 click变色
    纯CSS圆角框3-圆角化图片
    WINFORM自定义皮肤制作(上)
    EyesBaby1.0使用帮助文档
    C#实现小写金额转大写金额
    在winform中运用FusionCharts图表(一)
    第一章、基本的圆角框
  • 原文地址:https://www.cnblogs.com/rsapaper/p/8944790.html
Copyright © 2011-2022 走看看