How to get date and time from another timezone in Python?

How to Get Date and Time from Another Timezone in Python

Handling timezones correctly is one of the hardest parts of programming. In 2026, the “naive” datetime objects are considered a bad practice.

The Modern Way: zoneinfo (Python 3.9+)

You no longer need the third-party pytz library for most tasks. Python now includes zoneinfo in the standard library.

from datetime import datetime
from zoneinfo import ZoneInfo

utc_time = datetime.now(ZoneInfo("UTC"))
ny_time = utc_time.astimezone(ZoneInfo("America/New_York"))
print(ny_time)

Why This Matters

  • DST Handling: The zoneinfo database automatically handles Daylight Saving Time transitions, which change every year.
  • Database Storage: The 2026 standard is to always store timestamps in UTC in your database and only convert to the user’s local timezone when displaying the data in the UI.

Similar Posts

  • Micosoft招聘部分算法题

    Micosoft招聘部分算法题 1.链表和数组的区别在哪里? 2.编写实现链表排序的一种算法。说明为什么你会选择用这样的方法? 3.编写实现数组排序的一种算法。说明为什么你会选择用这样的方法? 4.请编写能直接实现strstr()函数功能的代码。 5.编写反转字符串的程序,要求优化速度、优化空间。 6.在链表里如何发现循环链接? 7.给出洗牌的一个算法,并将洗好的牌存储在一个整形数组里。 8.写一个函数,检查字符是否是整数,如果是,返回其整数值。(或者:怎样只用4行代码编写出一个从字符串到长整形的函数?) 9.给出一个函数来输出一个字符串的所有排列。 10.请编写实现malloc()内存分配函数功能一样的代码。 11.给出一个函数来复制两个字符串A和B。字符串A的后几个字节和字符串B的前几个字节重叠。 12.怎样编写一个程序,把一个有序整数数组放到二叉树中? 13.怎样从顶部开始逐层打印二叉树结点数据?请编程。 14.怎样把一个链表掉个顺序(也就是反序,注意链表的边界条件并考虑空链表)? 来源:·日月光华 bbs.fudan.edu.cn Read more: OCaml Learning Materials Vim Indenting C/C++ Code RAII-like Error Handling and Resource Management in C Fetching RSS Feed and Outputing Feed Items as HTML in PHP Handling Sparse Files on Linux Spring Shell Technology For Java…

  • How to Convert Integers to Strings and Strings to Integers in PHP

    How to Convert Integers to Strings and Strings to Integers in PHP Type conversion is a daily task in PHP, especially when handling user input from forms or APIs. While PHP is loosely typed, explicit conversion is a best practice for security and performance. 1. Integer to String Casting: (string) $myInt Interpolation: “$myInt” Function: strval($myInt)…

  • | | |

    How to convert Managed Solution into Unmanaged for On-Premise CRM organisation?

    Solution is very important part of Dynamics CRM. In order to deploy your customization, solution is the only bridge which help you to achieve your goal. There are two types of solutions available in CRM: Managed and Unmanaged. Managed Solutions: This is the solutions that you can import and publish only. You neither export it nor you can…

  • Excerpt in homepage, category, tag and author pages for WordPress theme Twenty Thriteen

    Excerpt in homepage, category, tag and author pages for WordPress theme Twenty Thriteen. Also add the “read more” link after the excerpt. The patch: diff –git a/wp-content/themes/twentythirteen/content.php b/wp-content/themes/twentythirteen/content.php index 4f61b22..53b4686 100644 — a/wp-content/themes/twentythirteen/content.php +++ b/wp-content/themes/twentythirteen/content.php @@ -30,7 +30,7 @@ </div><!– .entry-meta –> </header><!– .entry-header –> – <?php if ( is_search() ) : // Only display…