在python中以特定格式获取日期

时间:2016-04-29 00:19:29

标签: python

如何在foll中获取日期。 python中的格式:

2016-04-26T19:50:48Z

我这样做:

import datetime

now = datetime.now()
now.strftime("%Y %m %d %H:%M")

2 个答案:

答案 0 :(得分:6)

好吧,看起来您正试图以ISO 8601格式获取日期对象的字符串表示形式。我不知道你最后是否真的需要scala> // Step 1 scala> val inputList = List( "data=first data || key1=r1v1 || key2=", | "data=second data || key1=r2v1 || key2=r2v2" | ) inputList: List[String] = List(data=first data || key1=r1v1 || key2=, data=second data || key1=r2v1 || key2=r2v2) scala> // Step 2 scala> val splitted = inputList.map{ x => | x.split("\\|\\|") | .map(_.trim) | } splitted: List[Array[String]] = List(Array(data=first data, key1=r1v1, key2=), Array(data=second data, key1=r2v1, key2=r2v2)) scala> // Step 3 scala> val filteredList = splitted.map{ x => | val retval = for { element <- x | val keyNval = element.split("=") | if keyNval.size >= 2 | } yield { | val splitted = element.split("=") | val concatenated = splitted(0) + " -> " + splitted(1) | concatenated | } | retval | } warning: there were 1 deprecation warning(s); re-run with -deprecation for details filteredList: List[Array[String]] = List(Array(data -> first data, key1 -> r1v1), Array(data -> second data, key1 -> r2v1, key2 -> r2v2)) scala> // Step 4 scala> val dnryList = filteredList.map{ x => | x.toMap | } <console>:32: error: Cannot prove that String <:< (T, U). x.toMap ,这是另一种Z方式:

.strftime()

我相信你可以想出把>>> import datetime >>> now = datetime.datetime.now() >>> now.replace(microsecond=0).isoformat() '2016-04-28T17:29:53' 附加到字符串上。

答案 1 :(得分:5)

嗯,首先,你没有正确地获得now()。这是datetime.datetime中的内容,而不是顶级datetime。其次,您似乎并没有尝试获得您想要的格式字符串 - 它甚至没有您指定的破折号。

>>> import datetime
>>> now = datetime.now()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'now'
>>> now = datetime.datetime.now()
>>> now.strftime("%Y %m %d %H:%M")
'2016 04 28 17:20'
>>> now.strftime("%Y-%m-%dT%H:%M:%SZ")
'2016-04-28T17:20:09Z'