如何将给定日期转换为America/Los_Angeles时区时间?
First, let's clarify the context: Your checkIn value "2021-08-30 03:45:00" is likely in UTC. Since August falls within Daylight Saving Time (DST) for America/Los_Angeles, the applicable timezone offset is UTC-7 (PDT).
The converted local time for America/Los_Angeles would be 2021-08-29 20:45:00 PDT.
If you need to implement this programmatically, here are practical examples in common languages:
Python Example
Using the datetime and pytz libraries (install pytz first with pip install pytz):
from datetime import datetime import pytz # Parse the UTC check-in time utc_time = datetime.strptime("2021-08-30 03:45:00", "%Y-%m-%d %H:%M:%S").replace(tzinfo=pytz.utc) # Convert to America/Los_Angeles timezone la_time = utc_time.astimezone(pytz.timezone("America/Los_Angeles")) print(la_time.strftime("%Y-%m-%d %H:%M:%S %Z")) # Output: 2021-08-29 20:45:00 PDT
JavaScript Example (Node.js/Browser)
Using vanilla JavaScript's Intl.DateTimeFormat and Date object:
// Parse the UTC time string const utcDate = new Date("2021-08-30T03:45:00Z"); // Format for America/Los_Angeles timezone const laFormatter = new Intl.DateTimeFormat("en-US", { timeZone: "America/Los_Angeles", year: "numeric", month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", second: "2-digit", timeZoneName: "short" }); console.log(laFormatter.format(utcDate)); // Output: 08/29/2021, 08:45:00 PM PDT
Java Example
Using Java 8+ java.time API:
import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.ZoneId; public class TimeConversion { public static void main(String[] args) { // Parse UTC time ZonedDateTime utcTime = ZonedDateTime.parse("2021-08-30T03:45:00Z"); // Convert to America/Los_Angeles ZonedDateTime laTime = utcTime.withZoneSameInstant(ZoneId.of("America/Los_Angeles")); // Format the output DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"); System.out.println(formatter.format(laTime)); // Output: 2021-08-29 20:45:00 PDT } }
All these examples automatically handle the DST adjustment for the given date, ensuring you get the accurate local time for America/Los_Angeles.
内容的提问来源于stack exchange,提问作者vims

