strftime

How do I strftime a date object in a different locale?

旧时模样 提交于 2019-11-26 12:43:03
问题 I have a date object in python and I need to generate a time stamp in the C locale for a legacy system, using the %a (weekday) and %b (month) codes. However I do not wish to change the application\'s locale, since other parts need to respect the user\'s current locale. Is there a way to call strftime() with a certain locale? 回答1: The example given by Rob is great, but isn't threadsafe. Here's a version that works with threads: import locale import threading from datetime import datetime from

Python strftime - date without leading 0?

不想你离开。 提交于 2019-11-26 04:05:30
问题 When using Python strftime , is there a way to remove the first 0 of the date if it\'s before the 10th, ie. so 01 is 1 ? Can\'t find a % thingy for that? Thanks! 回答1: Actually I had the same problem and I realized that, if you add a hyphen between the % and the letter, you can remove the leading zero. For example %Y/%-m/%-d . This only works on Unix (Linux, OS X), not Windows (including Cygwin). On Windows, you would use # , e.g. %Y/%#m/%#d . 回答2: We can do this sort of thing with the advent

YYYY-MM-DD format date in shell script

情到浓时终转凉″ 提交于 2019-11-26 02:59:23
问题 I tried using $(date) in my bash shell script, however I want the date in YYYY-MM-DD format. How do I get this? 回答1: In bash (>=4.2) it is preferable to use printf's built-in date formatter (part of bash) rather than the external date (usually GNU date). As such: # put current date as yyyy-mm-dd in $date # -1 -> explicit current date, bash >=4.3 defaults to current time if not provided # -2 -> start time for shell printf -v date '%(%Y-%m-%d)T\n' -1 # put current date as yyyy-mm-dd HH:MM:SS in

How to change the datetime format in pandas

我是研究僧i 提交于 2019-11-26 01:29:44
问题 DOB column sample value is in the format - 1/1/2016 which by default gets converted to object as shown below DOB object Converting to date format df[\'DOB\'] = pd.to_datetime(df[\'DOB\']) Date converts to 2016-01-26 dtype is DOB datetime64[ns] Now I want to convert this date format to 01/26/2016 or in any other general date formats. How do I do it? Whatever the method I try it always shows the date in 2016-01-26 format. 回答1: You can use dt.strftime if you need to convert datetime to other

Convert python datetime to epoch with strftime

回眸只為那壹抹淺笑 提交于 2019-11-26 01:25:08
问题 I have a time in UTC from which I want the number of seconds since epoch. I am using strftime to convert it to the number of seconds. Taking 1st April 2012 as an example. >>>datetime.datetime(2012,04,01,0,0).strftime(\'%s\') \'1333234800\' 1st of April 2012 UTC from epoch is 1333238400 but this above returns 1333234800 which is different by 1 hour. So it looks like that strftime is taking my system time into account and applies a timezone shift somewhere. I thought datetime was purely naive?

Converting unix timestamp string to readable date

眉间皱痕 提交于 2019-11-25 22:45:32
问题 I have a string representing a unix timestamp (i.e. \"1284101485\") in Python, and I\'d like to convert it to a readable date. When I use time.strftime , I get a TypeError : >>>import time >>>print time.strftime(\"%B %d %Y\", \"1284101485\") Traceback (most recent call last): File \"<stdin>\", line 1, in <module> TypeError: argument must be 9-item sequence, not str 回答1: Use datetime module: from datetime import datetime ts = int("1284101485") # if you encounter a "year is out of range" error