Python在gae日期时间到UTC格式

时间:2021-09-09 21:30:37

I have a datetime on my model

我的模特上有一个日期时间

2013-02-19 05:29:27.874648

I am using jquery timeago.. but the problem is it don't give the desire output..

我正在使用jquery timeago ..但问题是它没有给出欲望输出..

<time class="timeago" datetime="{{ activity.created }}">{{ activity.date }}</time>

the code above is my sample html code..

上面的代码是我的示例HTML代码..

here's my javascript :

这是我的javascript:

<script>
    jQuery(document).ready(function() {
      jQuery("time.timeago").timeago();
    });
</script>

my question is.. should I convert it to UTC format??.. If that so! how do I convert it?.. convert it in python way or javascript??.. how do I do it?.

我的问题是..我应该将其转换为UTC格式吗?如果是这样的话!我怎么转换它?..用python方式或javascript转换它?我该怎么做?

2 个解决方案

#1


0  

If i understood your question then .

如果我理解你的问题那么。

How can I convert in UTC

如何转换为UTC

from datetime import datetime
from pytz import timezone

date_str = "2009-05-05 22:28:15"
datetime_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
datetime_obj_utc = datetime_obj.replace(tzinfo=timezone('UTC'))
print datetime_obj_utc.strftime("%Y-%m-%d %H:%M:%S %Z%z")

You need to install pytz

你需要安装pytz

If you want to add some timestamp in your current time .

如果要在当前时间添加一些时间戳。

Do something like this

做这样的事情

future = datetime.datetime.now() + datetime.timedelta(minutes=5)
return time.mktime(future.timetuple()).

Please go through this article time.mktime

请仔细阅读本文time.mktime

#2


0  

You generally want the date to already be in UTC (or explicitly offset from UTC) on the server, then converted at the last second (pun intended) to the client's local timezone.

您通常希望日期在服务器上已经是UTC(或明确偏离UTC),然后在最后一秒(双关语)转换为客户端的本地时区。

With jquery-timeago, assuming the date is already (relative to) UTC, you can format it as such (in Python, server-side) and jquery-timeago should figure out the rest correctly.

使用jquery-timeago,假设日期已经(相对于)UTC,您可以将其格式化(在Python中,服务器端),jquery-timeago应该正确地找出其余部分。

Here's what I use to format UTC dates in my own HTML templates:

以下是我在自己的HTML模板中格式化UTC日期的方法:

{{ activity.date.isoformat() + 'Z' }}

#1


0  

If i understood your question then .

如果我理解你的问题那么。

How can I convert in UTC

如何转换为UTC

from datetime import datetime
from pytz import timezone

date_str = "2009-05-05 22:28:15"
datetime_obj = datetime.strptime(date_str, "%Y-%m-%d %H:%M:%S")
datetime_obj_utc = datetime_obj.replace(tzinfo=timezone('UTC'))
print datetime_obj_utc.strftime("%Y-%m-%d %H:%M:%S %Z%z")

You need to install pytz

你需要安装pytz

If you want to add some timestamp in your current time .

如果要在当前时间添加一些时间戳。

Do something like this

做这样的事情

future = datetime.datetime.now() + datetime.timedelta(minutes=5)
return time.mktime(future.timetuple()).

Please go through this article time.mktime

请仔细阅读本文time.mktime

#2


0  

You generally want the date to already be in UTC (or explicitly offset from UTC) on the server, then converted at the last second (pun intended) to the client's local timezone.

您通常希望日期在服务器上已经是UTC(或明确偏离UTC),然后在最后一秒(双关语)转换为客户端的本地时区。

With jquery-timeago, assuming the date is already (relative to) UTC, you can format it as such (in Python, server-side) and jquery-timeago should figure out the rest correctly.

使用jquery-timeago,假设日期已经(相对于)UTC,您可以将其格式化(在Python中,服务器端),jquery-timeago应该正确地找出其余部分。

Here's what I use to format UTC dates in my own HTML templates:

以下是我在自己的HTML模板中格式化UTC日期的方法:

{{ activity.date.isoformat() + 'Z' }}