strftime

使用Python将字符串转换为格式化的日期时间字符串

a 夏天 提交于 2019-11-29 03:08:27
我正在尝试将字符串“20091229050936”转换为“2009年12月29日(UTC)” >>>import time >>>s = time.strptime("20091229050936", "%Y%m%d%H%M%S") >>>print s.strftime('%H:%M %d %B %Y (UTC)') 给 AttributeError: 'time.struct_time' object has no attribute 'strftime' 显然,我犯了一个错误:时间错了,它是一个日期时间对象! 它有一个日期 和 时间组件! >>>import datetime >>>s = datetime.strptime("20091229050936", "%Y%m%d%H%M%S") 给 AttributeError: 'module' object has no attribute 'strptime' 我是怎么意思将字符串转换为格式化的日期字符串? 解决方案 time.strptime 返回 time_struct ; time.strftime 接受a time_struct 作为可选参数: >>>s = time.strptime(page.editTime(), "%Y%m%d%H%M%S") >>>print time.strftime('%H:%M %d

Use datetime.strftime() on years before 1900? (“require year >= 1900”)

心不动则不痛 提交于 2019-11-28 22:39:53
I used : utctime = datetime.datetime(1601,1,1) + datetime.timedelta(microseconds = tup[5]) last_visit_time = "Last visit time:"+ utctime.strftime('%Y-%m-%d %H:%M:%S') But I have the time of 1601, so the error show: ValueError: year=1601 is before 1900; the datetime strftime() methods require year >= 1900 I used python2.7, how can I make it? Thanks a lot! You can do the following: >>> utctime.isoformat() '1601-01-01T00:00:00.000050' Now if you want to have exactly the same format as above: iso = utctime.isoformat() tokens = iso.strip().split("T") last_visit_time = "Last visit time: %s %s" %

C++11 alternative to localtime_r

▼魔方 西西 提交于 2019-11-28 20:08:06
C++ defines time formatting functions in terms of strftime , which requires a struct tm "broken-down time" record. However, the C and C++03 languages provide no thread-safe way to obtain such a record; there is just one master struct tm for the whole program. In C++03, this was more or less OK, because the language didn't support multithreading; it merely supported platforms supporting multithreading, which then provided facilities like POSIX localtime_r . C++11 also defines new time utilities, which interface with the non-broken-down time_t type, which is what would be used to reinitialize

How to format a time stamp in C

人盡茶涼 提交于 2019-11-28 12:06:20
问题 I'm trying to figure out how to get the current timestamp using the function below but I want to format it so that it displays the time like 4:30:23 on output. Eventually I want to subtract the time stamps before and after I run an algorithm. struct timeval FindTime() { struct timeval tv; gettimeofday(&tv,NULL); return tv; } int main() { printf("%ld\n",FindTime()); return0; } Current output format: 1456178100 回答1: Could be this what you need? #include <time.h> #include <stdlib.h> #include

How to convert week number and year into unix timestamp?

喜欢而已 提交于 2019-11-28 11:52:49
I'm trying to group together dates into a week number and year, and then I want to convert that week number back into a unix timestamp. How can I go about doing this? I assume you are using ISO 8601 week numbers, and want the first day of a ISO 8601 week so that e.g. Week 1 of 2011 returns January 3 2011 . strtotime can do this out of the box using the {YYYY}W{WW} format: echo date("Y-m-d", strtotime("2011W01")); // 2011-01-03 Note that the week number needs to be two digits. Shamefully, DateTime::createFromFormat , the fancy new PHP 5 way of dealing with dates, seems unable to parse this kind

day18

孤街浪徒 提交于 2019-11-28 09:46:57
1. random模块 1.1 基础方法   import random # (1) 取随机小数: 数学计算 print(random.random()) # 取0-1之间的小数 print(random.uniform(1, 2)) # 取所给范围之间的小数 # (2) 取随机整数: 彩票 抽奖 print(random.randint(1, 2)) # [1,2] 顾头也顾尾 print(random.randrange(1, 2)) # [1,2) 顾头不顾尾 print(random.randrange(1, 200, 2)) # 每两个取一个(200以内的奇数) # (3) 从一个列表中随机抽取值: 抽奖 li = ['a', 'b', (1, 2), 123] print(random.choice(li)) # 随机取一个 print(random.sample(li, 2)) # 随机取两个 # (4) 洗牌 打乱一个列表的顺序(没有返回值,在原来的列表基础上直接进行修改,节省空间) li = ['a', 'b', (1, 2), 123] random.shuffle(li) print(li) 1.2 验证码 - 课上练习 # 随机数练习 # (1) 4位 数字验证码 # (2) 6位 数字验证码 # (3) 6位 数字+字母验证码 # (1) 4位 数字验证码

Display the date, like “May 5th”, using pythons strftime? [duplicate]

瘦欲@ 提交于 2019-11-28 07:25:40
Possible Duplicate: Python: Date Ordinal Output? In Python time.strftime can produce output like "Thursday May 05" easily enough, but I would like to generate a string like "Thursday May 5th" (notice the additional "th" on the date). What is the best way to do this? Acorn strftime doesn't allow you to format a date with a suffix. Here's a way to get the correct suffix: if 4 <= day <= 20 or 24 <= day <= 30: suffix = "th" else: suffix = ["st", "nd", "rd"][day % 10 - 1] found here Update: Combining a more compact solution based on Jochen's comment with gsteff's answer : from datetime import

Why does “%-d”, or “%-e” remove the leading space or zero?

半世苍凉 提交于 2019-11-28 00:41:40
On SO question 904928 (Python strftime - date without leading 0?) Ryan answered: Actually I had the same problem and I realised that, if you add a hyphen between the % and the letter, you can remove the leading zero. For example %Y/%-m/%-d. I faced the same problem and that was a great solution, BUT, why does this behave like this? >>> import datetime >>> datetime.datetime(2015, 3, 5).strftime('%d') '05' >>> datetime.datetime(2015, 3, 5).strftime('%-d') '5' # It also works with a leading space >>> datetime.datetime(2015, 3, 5).strftime('%e') ' 5' >>> datetime.datetime(2015, 3, 5).strftime('%-e

Converting a string to a formatted date-time string using Python

牧云@^-^@ 提交于 2019-11-27 19:46:17
I'm trying to convert a string "20091229050936" into "05:09 29 December 2009 (UTC)" >>>import time >>>s = time.strptime("20091229050936", "%Y%m%d%H%M%S") >>>print s.strftime('%H:%M %d %B %Y (UTC)') gives AttributeError: 'time.struct_time' object has no attribute 'strftime' Clearly, I've made a mistake: time is wrong, it's a datetime object! It's got a date and a time component! >>>import datetime >>>s = datetime.strptime("20091229050936", "%Y%m%d%H%M%S") gives AttributeError: 'module' object has no attribute 'strptime' How am I meant to convert a string into a formatted date-string? For

Swift: NSDate formatting with strftime & localtime

放肆的年华 提交于 2019-11-27 18:03:31
问题 How do I convert the following Objective-C code into Swift code? #define MAX_SIZE 11 char buffer[MAX_SIZE]; time_t time = [[NSDate date] timeIntervalSince1970]; strftime(buffer, MAX_SIZE, "%-l:%M\u2008%p", localtime(&time)); NSString *dateString = [NSString stringWithUTF8String:buffer]; NSLog(@"dateString: %@", dateString); // dateString: 11:56 PM I'm formatting a date . 回答1: As the commentators @BryanChen and @JasonCoco said, use NSDateFormatter. let dateFormatter = NSDateFormatter()