如何创建基本的时间戳或日期? (Python 3.4) [英] How can I create basic timestamps or dates? (Python 3.4)

查看:192
本文介绍了如何创建基本的时间戳或日期? (Python 3.4)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为初学者,创建时间戳或格式化日期最终比我预期的要多一个挑战。什么是一些基本的例子?

As a beginner, creating timestamps or formatted dates ended up being a little more of a challenge than I would have expected. What are some basic examples for reference?

推荐答案

最终你想查看datetime文档,熟悉格式化变量,但是以下是一些让您开始的例子:

Ultimately you want to review the datetime documentation and become familiar with the formatting variables, but here are some examples to get you started:

import datetime

print('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
print('Timestamp: {:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()))
print('Date now: %s' % datetime.datetime.now())
print('Date today: %s' % datetime.date.today())

today = datetime.date.today()
print("Today's date is {:%b, %d %Y}".format(today))

schedule = '{:%b, %d %Y}'.format(today) + ' - 6 PM to 10 PM Pacific'
schedule2 = '{:%B, %d %Y}'.format(today) + ' - 1 PM to 6 PM Central'
print('Maintenance: %s' % schedule)
print('Maintenance: %s' % schedule2)

输出:

时间戳:2014-10-18 21:31:12

Timestamp: 2014-10-18 21:31:12

时间戳:2014-Oct-18 21:31:12

Timestamp: 2014-Oct-18 21:31:12

现在日期:2014-10-18 21:31:12.318340

Date now: 2014-10-18 21:31:12.318340

日期今天:2014-10-18

Date today: 2014-10-18

今天的日期是2014年10月18日

Today's date is Oct, 18 2014

维护:2014年10月18日 - 下午6点至10点太平洋

Maintenance: Oct, 18 2014 - 6 PM to 10 PM Pacific

维护:2014年10月18日 - 下午1点至6点中央

Maintenance: October, 18 2014 - 1 PM to 6 PM Central

参考链接:https://docs.python.org/3.4/library/datetime.html#strftime-strptime-behavior

这篇关于如何创建基本的时间戳或日期? (Python 3.4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆