首页 > 编程语言 >python中的时间转换,秒级时间戳转string,string转时间

python中的时间转换,秒级时间戳转string,string转时间

时间:2024-04-20 09:04:00浏览次数:12  
标签:-% string python nowTime 时间 str time print def

import time
import datetime


def paserTime(timestamp):
    t = time.time()
    f=time.localtime(timestamp/1000)
    print (t)                       #原始时间数据
    # print (int(t))                  #秒级时间戳
    print (int(round(t * 1000)))    #毫秒级时间戳

    #nowTime = lambda: int(round(t * 1000))
  #  print(nowTime());  # 毫秒级时间戳,基于lambda
    nowTime = lambda:timestamp
    str=time.strftime('%Y-%m-%d %H:%M:%S',f)
    print(str)  # 日期格式化

    return str
# paserTime(time.time())
def now():
    nowTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')  # 现在
    print(nowTime)
def nowTime():
    t = time.time()
    f = time.localtime(int(t))
    # print(t)
    str = time.strftime('%Y-%m-%d %H:%M:%S', f)
    print(str)  # 日期格式化
    return str
# nowTime()
def long2Str(longTime):
    f = time.localtime(int(longTime))
    # print(t)
    str = time.strftime('%Y-%m-%d %H:%M:%S', f)
    print(str)  # 日期格式化
    return str
def str2time(str):
    date_time = datetime.datetime.strptime(str, '%Y-%m-%d %H:%M:%S')
    # print(date_time)
    return date_time

def str2timestamp(str):
    timstamp=time.mktime(time.strptime(str, '%Y-%m-%d %H:%M:%S'))
    # print(timstamp)
    return timstamp
    # time.mktime()   与    time.localtime() 互为还原函数。
    # time.mktime(timetuple) :将时间元组转换成时间戳
    # time.localtime([timestamp]):将时间戳转会为时间元组
# print(str2timestamp(str2)-str2timestamp(str))```


 


![](https://mutouzuo.oss-cn-hangzhou.aliyuncs.com/my/mudouzuo1.png)

标签:-%,string,python,nowTime,时间,str,time,print,def
From: https://www.cnblogs.com/bigleft/p/18147144

相关文章

  • 利用python收发MQ
    一、收发MQ基础引入stomp包,#-*-coding:utf-8-*-importstompimporttime,randomfromcmd.utilimporttimePaserport=700ip='10.13.69.16'passcode='8JiPUndN'queue_name='/topic/SampleQueue'topic_name='/topic/cweb.t......
  • 涛思数据 TDengine 征稿— 利用python脚本做TDengine性能测试
    【本文正在参与“拥抱开源|涛思数据TDengine有奖征稿】https://marketing.csdn.net/p/0ada836ca30caa924b9baae0fd33857c设备上传的数据具有很明显的物联网特点,通常是时序性的,按时间先后顺序上报,而且写入后,几乎不会修改,主要是查询和统计。针对这些特点,时序数据库TDengine将......
  • 【开源】使用Python+Flask+Mysql快速开发一个用户增删改查系统
    项目演示项目本身很简单,增删改查是几乎所有系统的骨架。正所谓万丈高楼平地起,学会了增删改查,航母就指日可待了:),光速入门,直接看演示图:项目地址https://github.com/mudfish/python-flask-user-crudFlask框架介绍说白了就是一个Web框架,能够让你快速开发出Pythonweb应用。简单易......
  • Ubuntu 22.04 和 Windows 时间冲突解决方案
    默认情况下,Ubuntu(和大多数其他Linux发行版)假设硬件时钟设置为协调世界时间(UTC+0),而Windows则假设硬件时钟设置为当地时间,这导致Ubuntu快8小时。这种差异会导致你在双启动系统中切换操作系统时,经常遇到时间显示不正确的问题。要解决这个问题,有两种常用方法,要么让Linux......
  • python --二分法学习
    deffound_number(need_vaule,l):print(l)mid_index=len(l)//2mid_value=l[mid_index]print("mid_valueis%s"%(mid_value))ifmid_value>need_vaule:l=l[:mid_index]print('needtofind1')......
  • springboot java调用flask python写的
    服务a用flask,服务b用的springboot,服务a写的接口,用python很容易就调通了,java来调,坑有点多1、url最后的斜杠必须两边对应上,否则flask会先308,而且contenttype[text/html;charset=utf-8],连对应的HttpMessageConverter都没有org.springframework.web.client.RestClientException:......
  • python 正则表达式匹配
    re模块: 案例:     python的贪婪和非贪婪 r的作用: ......
  • python 修改jenkins的配置文件
    python有jenkins获取配置文件的api,也有修改配置文件的api,下面介绍下如果修改jenkinsjob的配置文件内容:importreimporttimeimportjenkinsjenkins_url="http://xxx.com/jenkins"username="zhangsan"token="1.......de"jenkins=jenkins.Jenkins(jenkins_url,us......
  • python多继承构造方法参数报错
    各路大神,今天下午在学习Python3.12多继承的时候,有个构造方法一直报错,希望大家能帮忙瞅瞅,求求了~~~~~~~代码如下:点击查看代码classRectangle:def__init__(self,width,height):self.width=widthself.height=heightdefarea(self):......
  • python minio
    fromminioimportMiniofile_name='3e09ca66d9444906935b0171e26891f1.mp4'file_path=r'E:\集成资料\视频素材'barrel="testdata"defupload_file():#创建minio客户端client=Minio(endpoint="xxx.xxx.xxx.xxx:xxxxx"......