比较python json中的2个时间戳

时间:2018-06-26 06:57:34

标签: python json timestamp

[{"id":360015384872,"action":"article_created","timestamp":"2018-06-26T05:43:05Z","breadcrumbs"]

让我说我有时间戳记:2018-06-26T05:43:05Z

和另一个时间戳记2:2018-06-26T05:43:55Z-这个电流比50秒多

我想这样做,以便如果timestamp2> timestamp1(意味着timestamp2是最近的时间或当前时间),则打印(timestamp2)

我如何在python json中做到这一点?我知道如果它是timestamp2> timestamp1 ..的整数怎么办,但是使用timestamp似乎更困难

2 个答案:

答案 0 :(得分:0)

您可以将这些日期字符串转换为datetime对象,然后轻松进行比较。我建议<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache-core</artifactId> <version>2.6.6</version> </dependency> 用于轻松解析这些日期字符串。您可以通过执行dateutil.parser

来获得它
pip install python-dateutil

答案 1 :(得分:0)

您可以使用<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:layout_margin="20dp" android:background="@color/colorWhite" android:orientation="vertical" android:padding="5dp"> <TextView android:id="@+id/deskART" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="5dp" android:text="VARIANTI" android:textColor="#008b58" android:textSize="20sp" android:textStyle="bold" tools:ignore="HardcodedText" /> <SearchView android:id="@+id/searchMenu" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/TitleBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/desc" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginBottom="2dp" android:layout_marginEnd="2dp" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:layout_marginStart="2dp" android:layout_marginTop="2dp" android:layout_weight="2" android:text="Descrizione" tools:ignore="HardcodedText" /> <TextView android:paddingEnd="10dp" android:id="@+id/preMeno" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="2dp" android:layout_weight="1" android:text="+ -" android:textAlignment="textEnd" tools:ignore="HardcodedText,RtlSymmetry" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:focusable="true" android:focusableInTouchMode="true"> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerViewVarianti" android:layout_width="match_parent" android:layout_height="400dp" android:scrollbars="vertical" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:focusable="true" android:focusableInTouchMode="true"> <ImageButton android:id="@+id/conBTN" android:layout_width="0dp" android:layout_height="50dp" android:layout_margin="2dp" android:background="#008b58" android:layout_weight="1" android:padding="5dp" android:scaleType="fitCenter" android:src="@drawable/convar" android:tint="@color/colorWhite" tools:ignore="ContentDescription" /> <ImageButton android:id="@+id/piuBTN" android:layout_width="0dp" android:layout_height="50dp" android:layout_margin="2dp" android:layout_weight="1" android:background="#008b58" android:padding="5dp" android:scaleType="fitCenter" android:src="@drawable/piu" android:tint="@color/colorWhite" tools:ignore="ContentDescription" /> <ImageButton android:id="@+id/menoBTN" android:layout_width="0dp" android:layout_height="50dp" android:layout_margin="2dp" android:layout_weight="1" android:background="#008b58" android:padding="5dp" android:scaleType="fitCenter" android:src="@drawable/meno" android:tint="@color/colorWhite" tools:ignore="ContentDescription" /> <ImageButton android:id="@+id/senzaBTN" android:layout_width="0dp" android:layout_height="50dp" android:layout_margin="2dp" android:layout_weight="1" android:background="#008b58" android:padding="5dp" android:scaleType="fitCenter" android:src="@drawable/senza" android:tint="@color/colorWhite" tools:ignore="ContentDescription" /> </LinearLayout> </LinearLayout> 模块将字符串转换为datetime对象,然后进行比较。

例如:

datetime

输出:

import datetime
t1 = "2018-06-26T05:43:05Z"
t2 = "2018-06-26T05:43:55Z"
t1 = datetime.datetime.strptime(t1, "%Y-%m-%dT%H:%M:%SZ")
t2 = datetime.datetime.strptime(t2, "%Y-%m-%dT%H:%M:%SZ")

if t2 > t1:
    print(t2)