如何在Dart中打印UTC、欧洲及美洲时区的时间与当前时间戳?
Got it, let's break down how to tackle your two tasks in Dart—displaying timezone details for UTC, Europe, and the Americas, plus generating timestamps and converting them to those timezones.
First, you’ll need the intl package since Dart’s core DateTime doesn’t handle timezone conversions out of the box. Add this to your pubspec.yaml:
dependencies: intl: ^0.18.1 # Grab the latest version when you implement this
1. Print Timezone Information
We’ll use standard IANA timezone identifiers (like Europe/Berlin for Central European Time, America/New_York for Eastern Time) to fetch key details like UTC offsets and timezone abbreviations. Here’s how:
import 'package:intl/intl.dart'; void printTimezoneDetails() { // Define the target timezones const targetTimezones = [ 'UTC', 'Europe/Berlin', 'America/New_York', ]; print('=== Timezone Information ==='); for (var tz in targetTimezones) { final offset = TimeZoneOffset(tz).offset; final offsetHours = offset.inHours; // Get current timezone abbreviation (accounts for DST) final tzAbbreviation = DateFormat.timeZoneName.format( DateTime.now().toUtc().add(offset) ); print('$tz:'); print(' UTC Offset: ${offsetHours >= 0 ? '+' : ''}$offsetHours hours'); print(' Current Abbreviation: $tzAbbreviation\n'); } }
2. Generate Timestamp & Convert to Target Timezones
First, generate a timestamp (we’ll use milliseconds since the Unix epoch, a common standard). Then convert that timestamp to readable times in your three target timezones:
void printCurrentTimesWithTimestamp() { // Generate current timestamp (milliseconds since Unix epoch) final currentTimestamp = DateTime.now().millisecondsSinceEpoch; print('Current Timestamp (ms since epoch): $currentTimestamp\n'); // Convert timestamp to UTC DateTime first final utcDateTime = DateTime.fromMillisecondsSinceEpoch( currentTimestamp, isUtc: true ); // Define a readable time format final timeFormatter = DateFormat('yyyy-MM-dd HH:mm:ss zzz'); print('=== Current Time in Target Timezones ==='); // UTC Time print('UTC: ${timeFormatter.format(utcDateTime)}'); // Europe/Berlin Time final europeTime = utcDateTime.add(TimeZoneOffset('Europe/Berlin').offset); print('Europe (Berlin): ${timeFormatter.format(europeTime)}'); // America/New_York Time final americaTime = utcDateTime.add(TimeZoneOffset('America/New_York').offset); print('America (New York): ${timeFormatter.format(americaTime)}'); }
Full Working Example
Put everything together and run the main function:
import 'package:intl/intl.dart'; void main() { printTimezoneDetails(); printCurrentTimesWithTimestamp(); } void printTimezoneDetails() { const targetTimezones = [ 'UTC', 'Europe/Berlin', 'America/New_York', ]; print('=== Timezone Information ==='); for (var tz in targetTimezones) { final offset = TimeZoneOffset(tz).offset; final offsetHours = offset.inHours; final tzAbbreviation = DateFormat.timeZoneName.format( DateTime.now().toUtc().add(offset) ); print('$tz:'); print(' UTC Offset: ${offsetHours >= 0 ? '+' : ''}$offsetHours hours'); print(' Current Abbreviation: $tzAbbreviation\n'); } } void printCurrentTimesWithTimestamp() { final currentTimestamp = DateTime.now().millisecondsSinceEpoch; print('Current Timestamp (ms since epoch): $currentTimestamp\n'); final utcDateTime = DateTime.fromMillisecondsSinceEpoch( currentTimestamp, isUtc: true ); final timeFormatter = DateFormat('yyyy-MM-dd HH:mm:ss zzz'); print('=== Current Time in Target Timezones ==='); print('UTC: ${timeFormatter.format(utcDateTime)}'); final europeTime = utcDateTime.add(TimeZoneOffset('Europe/Berlin').offset); print('Europe (Berlin): ${timeFormatter.format(europeTime)}'); final americaTime = utcDateTime.add(TimeZoneOffset('America/New_York').offset); print('America (New York): ${timeFormatter.format(americaTime)}'); }
Quick Notes
- Timezone Accuracy: Use IANA identifiers (like
Europe/ParisorAmerica/Los_Angeles) to automatically handle daylight saving time changes. - Dependency Setup: Don’t forget to run
dart pub getafter adding theintlpackage to yourpubspec.yaml. - Timestamp Options: If you need microseconds instead of milliseconds, replace
millisecondsSinceEpochwithmicrosecondsSinceEpoch.
内容的提问来源于stack exchange,提问作者Akshay Jadhav

