如何从日期对象python获取以毫秒(秒后3位小数)为单位的时间值?

要获取具有毫秒(秒后3位小数)的日期字符串,请使用以下命令:

from datetime import datetime

print datetime.utcnow().strftime(‘%Y-%m-%d %H:%M:%S.%f‘)[:-3]

输出2018-10-04 10:18:32.926

%f 显示毫秒

要在python中获得毫秒,请使用以下代码。

import datetime
#  获得当前时间
now=datetime.datetime.now() #2019-04-11 14:18:41.629019
print(now)

# 转换为指定的格式:
otherStyleTime = now.strftime("%Y-%m-%d %H:%M:%S")#2019-04-11 14:18:41
print(otherStyleTime)
#

输出如下所示

  2019-03-11 17:34:28.290409

原文地址:https://www.cnblogs.com/xyao1/p/10689413.html

时间: 2024-07-31 06:53:40

如何从日期对象python获取以毫秒(秒后3位小数)为单位的时间值?的相关文章

python获取大于2秒的请求行,并且重新输出到新的文件里

原文地址:http://blog.51cto.com/6850242/2125862

python 获取subprocess进程执行后返回值

test.py #coding=utf-8 import subprocess compilePopen = subprocess.Popen('gcc haha',shell=True,stderr=subprocess.PIPE) compilePopen.wait() print('the status code is:',compilePopen.returncode) with open('log','w') as object: object.write(compilePopen.s

日期对象

js日期对象 日期对象是js中的一个内置方法,用来操作日期与时间. 语法: 1 new Date(); 参数: 1.new Date(); 没有参数:以本机的时间做为参考,返回一个时间对象. 1 var sj=new Date(); 2 console.log(sj);//Thu Jul 28 2016 17:10:55 GMT+0800 (中国标准时间) 2.new Date(年,月,日,时,分,秒); 1 var sj=new Date(1999,1,1,1,1,1);//参数为数字 2 c

python 获取对象信息

当我们拿到一个对象的引用时,如何知道这个对象是什么类型.有哪些方法呢? 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type()判断: >>> type(123) <type 'int'> >>> type('str') <type 'str'> >>> type(None) <type 'NoneType'> 如果一个变量指向函数或者类,也可以用type()判断: >&

常用封装--Date篇--获取格式化的日期对象

虽然日期对象可以使用new Date()来获取,但是对于其格式却必须进行相应的转换,才能成为开发者想要的格式. 这里提供了一个封装的方法,通过结合正则表达式的使用,达到了可以对时间对象进行处理,生成多种日期格式的目的. function dateFormat(date,format) {var o = {"M+" : date.getMonth()+1, //month"d+" : date.getDate(), //day"h+" : date

python获取对象信息

获取对象信息 拿到一个变量,除了用 isinstance() 判断它是否是某种类型的实例外,还有没有别的方法获取到更多的信息呢? 例如,已有定义: class Person(object): def __init__(self, name, gender): self.name = name self.gender = gender class Student(Person): def __init__(self, name, gender, score): super(Student, sel

python datatime日期和时间值模块

datetime.time():是一个时间类,这个类接受4个参数,分别代表时,分,秒,毫秒.参数的默认值是为0 1 #!/usr/bin/env python 2 #coding:utf8 3 import datetime 4 t=datetime.time(20, 00, 13, 00) 5 print t 6 print '*'*20 7 print t.hour 8 print t.minute 9 print t.second 10 print t.microsecond 11 12

js 日期对象Date以及传参

创建一个日期对象,日期对象可传参new Date() 创建日期对象getFullYear() 获取年份 getMonth() 获取月份 返回值是 0(一月) 到 11(十二月) 之间的一个整数 getDate() 获取日期(多少号) getDay() 获取星期数 返回值是 0(周日) 到 6(周六) 之间的一个整数. getHours() 获取小时 返回值是 0 (午夜) 到 23 (晚上 11 点)之间的一个整数 getMinutes() 获取分钟数 返回值是 0 ~ 59 之间的一个整数 g

API--DateFormat-字符串和日期对象之间的转换

java.text 类 DateFormat public abstract class DateFormat  extends Format DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间.各种日期时间格式器 日期格式器 public static final DateFormat getDateInstance() 获取日期格式器,该格式器具有默认语言环境的默认格式化风格. 返回:     日期格式器. public static final