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
相关产品推荐
相关产品推荐

