writemicrosecondr语言write函数用法怎么用

Python datetime to string without microsecond component - Stack Overflow
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
I'm adding UTC time strings to Bitbucket API responses that currently only contain Amsterdam (!) time strings. For consistency with the UTC time strings returned elsewhere, the desired format is
11:07:04 (followed by +00:00, but that's not germane).
What's the best way to create such a string (without a microsecond component) from a datetime instance with a microsecond component?
&&& import datetime
&&& print unicode(datetime.datetime.now())
11:13:39.278026
I'll add the best option that's occurred to me as a possible answer, but there may well be a more elegant solution.
Edit: I should mention that I'm not actually printing the current time – I used datetime.now to provide a quick example. So the solution should not assume that any datetime instances it receives will include microsecond components.
9,288104674
&&& datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
' 18:21:26'
284k58672651
&&& import datetime
&&& now = datetime.datetime.now()
&&& print unicode(now.replace(microsecond=0))
9,288104674
Keep the first 19 characters that you wanted via slicing:
&&& str(datetime.datetime.now())[:19]
' 14:37:50'
29.6k55585
This is the way I do it. ISO format:
import datetime
datetime.datetime.now().replace(microsecond=0).isoformat()
# Returns: 'T14:58:07'
You can replace the 'T' if you don't want ISO format:
datetime.datetime.now().replace(microsecond=0).isoformat(' ')
# Returns: ' 15:05:27'
11.1k36366
Yet another option:
&&& import time
&&& time.strftime("%Y-%m-%d %H:%M:%S")
' 11:31:28'
By default this uses local time, if you need UTC you can use the following:
&&& time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
' 18:32:20'
124k15169230
In Python 3.6:
&&& datetime.now().isoformat(' ', 'seconds')
' 14:41:33'
Since not all datetime.datetime instances have a microsecond component (i.e. when it is zero), you can partition the string on a "." and take only the first item, which will always work:
unicode(datetime.datetime.now()).partition('.')[0]
from datetime import datetime
now = datetime.now().strftime("%Y-%m-%d_%H:%M:%S.%f")
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
大榕树网络文章管理系统官方博客
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
阅读(206)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
在LOFTER的更多文章
loftPermalink:'',
id:'fks_085064',
blogTitle:'要注意的 MYSql 保留字',
blogAbstract:'采用 数据库MYsql ,数据库中一表取名group,结果在其他方面没有问题的情况下报错,数据不能插入,在查找数据时却不会报错
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}From Wikipedia, the free encyclopedia
This article needs additional citations for . Please help
by . Unsourced material may be challenged and removed. (July 2009) ()
This animation illustrates the generation of the debris and ejecta clouds after a spherical aluminum projectile impacts a thin aluminum plate at approximately 7 km/s. The frame interval is about 1 microsecond.
A microsecond is an
of time equal to one
(0.000001 or 10-6 or 1/1,000,000) of a . Its symbol is μs. One microsecond is to one second as one second is to 11.574 days.
A microsecond is equal to 1000
or 1/1,000 . Because the next
is 1000 times larger, measurements of 10-5 and 10-4 seconds are typically expressed as tens or hundreds of microseconds. A microsecond of sound signal sample (44.1 kHz, 2 channel, 24 bit, WAV) is typically stored on 4 um of , 2 bits per us per 4 um.
1 microsecond (1 ) – cycle time for
(1 MHz), the inverse unit. This corresponds to radio wavelength
band), as can be calculated by multiplying 1 us by the
(approximately 3.00×108 m/s) to determine the distance travelled.
1 microsecond – the length of time of a high-speed, commercial
flash (see ).
1.8 microseconds – the amount of time subtracted from the
as a result of the .
2 microseconds – the lifetime of a
2.68 microseconds – the amount of time subtracted from the Earth's day as a result of the .
3. microseconds – the time taken by
to travel one
in a vacuum
4.63 microseconds – a fifth (a 60th of a 60th of a 60th of a second)
5.4 microseconds – the time taken by light to travel one
(or radio waves point-to-point in a near vacuum)
8.01 microseconds - the time taken by light to travel one mile in typical
10 microseconds (μs) – cycle time for frequency , radio wavelength
18 microseconds – net amount per year that the length of the day lengthens, largely due to .
20.8 microseconds –
interval for digital audio with 48000 samples/s
22.7 microseconds –
interval for
audio (44100 samples/s)
38 microseconds – discrepancy in
time per day (compensated by clock speed) due to
50 microseconds – cycle time for highest
tone (20 kHz)
50 microseconds to read – the access latency for a modern solid state drive which holds non-volatile computer data
100 microseconds (0.1 ms) – cycle time for frequency
125 microseconds – sampling interval for telephone audio (8000 samples/s)
164 microseconds - half-life of -214
240 microseconds –
250 microseconds – cycle time for highest tone in
audio (4 kHz)[]
277.8 microseconds – a fourth (a 60th of a 60th of a second), used in astronomical calculations by
in 1000 and 1267 AD, respectively.
489.67 microseconds - time for light at a ;nm frequency to travel 100 km in a singlemode fiber optic cable (where speed of light is approximately 200 million meters per second due to internal reflectance).
The average human eye
takes 350,000 microseconds (just over 1/3 of one second).
The average human finger
takes 150,000 microseconds (just over 1/7 of one second).
illuminates for 1000 microseconds.
Standard camera
opens the shutter for 4000 microseconds or 4 milliseconds.
Buis, Alan (January 10, 2005). . NASA 2011.
MacDonald, Fiona. . ScienceAlert.
Richard Pogge. .
(1879). . Translated by Sachau C Edward. . pp. 147–149.  .
(2000) [1928]. . translator: BR Belle. . table facing page 231.  .
: Hidden categories:

我要回帖

更多关于 r语言write函数用法 的文章

 

随机推荐