You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何将Epoch时间戳转换为指定日期格式?

毫秒级Epoch时间戳转日期信息的实现方案

你给出的1696185000000是毫秒级Epoch时间戳,下面是几种主流编程语言的实现方式,直接生成你需要的月份、日期、星期格式:

JavaScript

利用原生Date对象直接处理毫秒级时间戳,通过本地化方法快速生成目标格式:

const timestamp = 1696185000000;
const date = new Date(timestamp);

// 完整月份名称(如September)
const fullMonth = date.toLocaleString('en-US', { month: 'long' });
// 短格式日期(如4 Sep)
const shortDate = date.toLocaleString('en-US', { day: 'numeric', month: 'short' });
// 完整星期名称(如Monday)
const fullWeekday = date.toLocaleString('en-US', { weekday: 'long' });

console.log(fullMonth); // September
console.log(shortDate); // 4 Sep
console.log(fullWeekday); // Monday

Python

Python的datetime模块默认处理秒级时间戳,需先将毫秒转成秒,再用strftime格式化:

from datetime import datetime

timestamp = 1696185000000
# 转换为秒级时间戳
dt = datetime.fromtimestamp(timestamp / 1000)

# 完整月份名称
full_month = dt.strftime('%B')
# 短格式日期(去掉前置0的话,用f"{dt.day} {dt.strftime('%b')}")
short_date = dt.strftime('%d %b').lstrip('0')
# 完整星期名称
full_weekday = dt.strftime('%A')

print(full_month)  # September
print(short_date)  # 4 Sep
print(full_weekday)  # Monday

Java

使用Java 8+的时间API,通过Instant和DateTimeFormatter处理:

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class TimestampConverter {
    public static void main(String[] args) {
        long timestamp = 1696185000000L;
        // 转换为本地时间(可替换ZoneId.of("UTC")指定时区)
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());

        DateTimeFormatter fullMonthFormatter = DateTimeFormatter.ofPattern("MMMM");
        DateTimeFormatter shortDateFormatter = DateTimeFormatter.ofPattern("d MMM");
        DateTimeFormatter fullWeekdayFormatter = DateTimeFormatter.ofPattern("EEEE");

        System.out.println(dateTime.format(fullMonthFormatter)); // September
        System.out.println(dateTime.format(shortDateFormatter)); // 4 Sep
        System.out.println(dateTime.format(fullWeekdayFormatter)); // Monday
    }
}

内容的提问来源于stack exchange,提问作者Sakthivel k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 16:42:18