将一年中的小数点转换为时间戳

编程入门 行业动态 更新时间:2024-10-27 20:27:18
本文介绍了将一年中的小数点转换为时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何将一年中某天的十进制表示形式转换为带有所有部分的时间戳,包括完整日期和时间?

How can I convert a decimal representation of a day in the year to a timestamp with all the parts, both the full date and the time?

例如,我的第一个小数点是 22.968530853511766 ,我希望它采用很好的时间戳格式。

For example, my first decimal is 22.968530853511766 and I want it in a nice timestamp format.

推荐答案

使用 timedelta()对象,其值作为 days 参数;将其添加到上一年的12月31日午夜:

Use a timedelta() object with your value as the days parameter; add it to midnight December 31st of the previous year:

from datetime import datetime, timedelta epoch = datetime(datetime.now().year - 1, 12, 31) result = epoch + timedelta(days=your_decimal)

Demo:

>>> from datetime import datetime, timedelta >>> epoch = datetime(datetime.now().year - 1, 12, 31) >>> epoch + timedelta(days=22.968530853511766) datetime.datetime(2015, 1, 22, 23, 14, 41, 65743) >>> print(epoch + timedelta(days=22.968530853511766)) 2015-01-22 23:14:41.065743

datetime 对象可以使用 datetime.strftime()方法;我依赖演示中 print()调用的默认 str()转换。

A datetime object can be formatted any number of ways with the datetime.strftime() method; I relied on the default str() conversion as called by print() in the demo.

更多推荐

将一年中的小数点转换为时间戳

本文发布于:2023-10-23 15:59:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1521319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数点   转换为   年中   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!