Google Drive API文件列表:createdTime与modifiedTime含义解析及按创建时间筛选文件方法
Hey Daniel, great question—let's clear up the confusion around these two fields and show you how to properly filter files by creation time.
What do createdTime and modifiedTime represent?
Let's break down each field clearly:
createdTime: This UTC timestamp (in RFC 3339 format) marks when the file was first added to Google Drive—whether that's by uploading a local file, creating it directly in Drive (like a new Google Doc), or copying an existing Drive file. This value never changes after the file is initially created/uploaded, no matter how many edits or metadata tweaks you make later.modifiedTime: This is the timestamp of the most recent change to the file, which can include:- Editing the file's content (e.g., typing in a Google Doc, updating a spreadsheet cell)
- Adjusting metadata (renaming the file, changing sharing permissions)
- Inheriting the last modified time from a local file you upload (this is exactly what's happening in your example!)
Why does createdTime come after modifiedTime in your response?
In your sample, the file was almost certainly a local file that was last edited on 2022-07-14 before being uploaded to Drive on 2022-07-18. Google Drive preserves the original file's last modified time as modifiedTime, while createdTime reflects when the file was added to Drive. This is expected behavior, not a bug!
How to filter files created in the last 12 hours
To list only files created in the last 12 hours, you'll use the createdTime field in the q parameter of the Drive API's files.list endpoint—this is the only reliable way to target files based on when they were added to Drive.
Step 1: Calculate the cutoff time
First, generate a UTC timestamp for 12 hours before the current time, formatted in RFC 3339 (e.g., 2024-05-20T03:00:00.000Z).
Step 2: Build the query
Use the > operator to filter files where createdTime is later than your cutoff time. Here's how it looks in practice:
- Raw API request
qparameter:createdTime > '2024-05-20T03:00:00.000Z' - If you want to exclude folders (to only list actual files), add an extra condition:
createdTime > '2024-05-20T03:00:00.000Z' and mimeType != 'application/vnd.google-apps.folder'
Example with Python client library
Here's a quick snippet to generate the dynamic cutoff time and run the query:
from googleapiclient.discovery import build from datetime import datetime, timedelta, timezone # Initialize Drive API service (you'll need to set up authentication first) service = build('drive', 'v3') # Calculate 12 hours ago in UTC, formatted for Drive API twelve_hours_ago = datetime.now(timezone.utc) - timedelta(hours=12) cutoff_time = twelve_hours_ago.isoformat(timespec='milliseconds').replace('+00:00', 'Z') # Build the query query = f"createdTime > '{cutoff_time}' and mimeType != 'application/vnd.google-apps.folder'" # Execute the request results = service.files().list(q=query, fields="files(createdTime, id, modifiedTime, name)").execute() files = results.get('files', []) # Print the results if not files: print('No files found created in the last 12 hours.') else: print('Files created in the last 12 hours:') for file in files: print(f"{file['name']} (ID: {file['id']})") print(f" Created: {file['createdTime']}, Modified: {file['modifiedTime']}")
Important note
Never use modifiedTime to filter for recently created files—since it can be older than createdTime (like in your upload scenario) or updated later if the file is edited, it won't give you accurate results for creation time.
内容的提问来源于stack exchange,提问作者Daniel

