dcc
2024-06-13 24e2c4a6983d3ed1c67080b460f7e28f5e2e55f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
 
dayjs.extend(utc);
dayjs.extend(timezone)
dayjs.tz.setDefault("America/New_York");
 
export const formatUtcTimestamp = (timestamp) => {
  return new Date(timestamp).toLocaleString();  //2023/11/11 09:43:52'
}
 
export const formatGmtTimestamp = (timestamp) => {
  return new Date(timestamp).toGMTString(); //  Sat, 11 Nov 2023 01:43:52 GMT'
}
 
// 获得客户端当前时区
export function getTimeZone() {
  return Intl.DateTimeFormat().resolvedOptions().timeZone
}
 
// 时间戳转换为本地时间
export const formatLocalTime = (timestamp) => {
 // timestamp格式为服务端utc时间戳
  return dayjs.unix(timestamp).format('YYYY-MM-DD HH:mm:ss');
}
 
export const formatUsTime = (timestamp) => {
  // 创建 Date 对象,将时间戳转换为指定时区的时间
  const date = new Date(timestamp);
  const timeZoneOffset = -5; // 时区偏移量,比如西八区就是-5
  const utc = date.getTime() + (date.getTimezoneOffset() * 60000); // 将本地时间转换为UTC时间
  const convertedDate = new Date(utc + (3600000 * timeZoneOffset)); // 转换为指定时区的时间
 
  // 格式化时间
  const year = convertedDate.getFullYear();
  const month = convertedDate.getMonth() + 1;
  const day = convertedDate.getDate();
  const hour = convertedDate.getHours();
  const minute = convertedDate.getMinutes();
  const second = convertedDate.getSeconds();
 
  return (`${year}-${month}-${day} ${hour}:${minute}:${second}`);
}
 
// 美国东部时间,utc-5
export const formatEasternUnixTime = (timestamp) => {
  //时间戳 (秒) 用这个
  return dayjs.unix(timestamp).tz('America/New_York').format("YYYY-MM-DD HH:mm:ss");
}
 
// 美国东部时间,utc-5
export const formatEasternTime = (timestamp) => {
  //时间戳 (毫秒) 用这个
  return dayjs.tz(timestamp).format("YYYY-MM-DD HH:mm:ss");
}
 
export const formatTimeStringHour = (timestamp) => {
  return dayjs.tz(timestamp).format('HH:mm')
}