问题重现:

通过接口从后端获取时间戳,在前端用JS格式化显示。发现Chrome在Mac和Windows下,对特定的时间段(1986年至1991年),的处理方式并不相同。

问题产生原因:

查询资料后发现,原来中国也曾经使用过夏令时。

1986年至1991年,中华人民共和国在全国范围实行了六年夏令时,每年从4月中旬的第一个星期日2时整(北京时间)到9月中旬第一个星期日的凌晨2时整(北京夏令时)。除1986
年因是实行夏令时的第一年,从5月4日开始到9月14日结束外,其它年份均按规定的时段施行。1992年4月5日后不再实行。

要避免此情况可在后端对时间进行处理,返回格式化后的日期字符串。

1

TimeZone.setDefault(TimeZone.getTimeZone("GMT+8:00"));

或在前端通过getTimezoneOffset检测夏令时,尝试网上找到的夏令时检测算法。(不推荐)

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

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

<!DOCTYPE html>

<html>

<head>

<title>DST Calculator</title>

<script type="text/javascript">

function DisplayDstSwitchDates()

{

var year = new Date().getYear();

if (year < 1000)

year += 1900;

var firstSwitch = 0;

var secondSwitch = 0;

var lastOffset = 99;

// Loop through every month of the current year

for (i = 0; i < 12; i++)

{

// Fetch the timezone value for the month

var newDate = new Date(Date.UTC(year, i, 0, 0, 0, 0, 0));

var tz = -1 * newDate.getTimezoneOffset() / 60;

// Capture when a timzezone change occurs

if (tz > lastOffset)

firstSwitch = i-1;

else if (tz < lastOffset)

secondSwitch = i-1;

lastOffset = tz;

}

// Go figure out date/time occurences a minute before

// a DST adjustment occurs

var secondDstDate = FindDstSwitchDate(year, secondSwitch);

var firstDstDate = FindDstSwitchDate(year, firstSwitch);

if (firstDstDate == null && secondDstDate == null)

return 'Daylight Savings is not observed in your timezone.';

else

return 'Last minute before DST change occurs in ' +

year + ': ' + firstDstDate + ' and ' + secondDstDate;

}

function FindDstSwitchDate(year, month)

{

// Set the starting date

var baseDate = new Date(Date.UTC(year, month, 0, 0, 0, 0, 0));

var changeDay = 0;

var changeMinute = -1;

var baseOffset = -1 * baseDate.getTimezoneOffset() / 60;

var dstDate;

// Loop to find the exact day a timezone adjust occurs

for (day = 0; day < 50; day++)

{

var tmpDate = new Date(Date.UTC(year, month, day, 0, 0, 0, 0));

var tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;

// Check if the timezone changed from one day to the next

if (tmpOffset != baseOffset)

{

var minutes = 0;

changeDay = day;

// Back-up one day and grap the offset

tmpDate = new Date(Date.UTC(year, month, day-1, 0, 0, 0, 0));

tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;

// Count the minutes until a timezone chnage occurs

while (changeMinute == -1)

{

tmpDate = new Date(Date.UTC(year, month, day-1, 0, minutes, 0, 0));

tmpOffset = -1 * tmpDate.getTimezoneOffset() / 60;

// Determine the exact minute a timezone change

// occurs

if (tmpOffset != baseOffset)

{

// Back-up a minute to get the date/time just

// before a timezone change occurs

tmpOffset = new Date(Date.UTC(year, month,

day-1, 0, minutes-1, 0, 0));

changeMinute = minutes;

break;

}

else

minutes++;

}

// Add a month (for display) since JavaScript counts

// months from 0 to 11

dstDate = tmpOffset.getMonth() + 1;

// Pad the month as needed

if (dstDate < 10) dstDate = "0" + dstDate;

// Add the day and year

dstDate += '/' + tmpOffset.getDate() + '/' + year + ' ';

// Capture the time stamp

tmpDate = new Date(Date.UTC(year, month,

day-1, 0, minutes-1, 0, 0));

dstDate += tmpDate.toTimeString().split(' ')[0];

return dstDate;

}

}

}

</script>

</head>

<body>

<script type="text/javascript">

document.write("Current date/time: " + new Date() + "<br />");

document.write(DisplayDstSwitchDates());

</script>

</body>

</html>

对中国标准时间(CST)和中国夏令时(CDT)的不同处理相关推荐

  1. 时间格式转换,转时间戳,转UTC,转中国标准时间

    问题:在实际开发中后端要求需要UTC格式 2022-11-30T16:00:00.000Z 而我拿到的格式是中国标准时间或2022-12 解决思路:把拿到的时间转为时间戳,在进行转格式 一.时间转换为 ...

  2. js中将中国标准时间格式、CST日期转换为yyyy-MM-dd HH:mm:ss格式总结

    1.Wed Dec 18 2019 17:30:30 GMT+0800 (中国标准时间)格式转换为yyyy-MM-dd HH:mm:ss格式 var dictTime = new Date(" ...

  3. js中将中国标准时间格式、CST日期转换为yyyy-MM-dd HH:mm:ss格式

    问题: 把Mon Aug 01 2022 00:00:00 GMT+0800 (中国标准时间)格式的日期转换为yyyy-MM-dd HH:mm:ss 解决方法: 1.定义转换方法 formatDate ...

  4. java拨钟,关于时间的那些事 - 中国也曾实行过夏令时,你知道吗?

    之前遇到过一个由于标准时间/夏令时时间转换引起的问题,这里记录下来分享给大家. 大家都知道,地球上按照经度分成24个时区,每个时区相差一个小时.一般来说每个国家法定的时间都对应一个时区,比如中国用的东 ...

  5. Jan. 1, 2020 at 7:47 a.m. GMT+8遇到这种时间,转换成中国标准时间

    Jan. 1, 2020 at 7:47 a.m. GMT+8转成中国标准时间 只需要将其变成这种形式:Jan 1 2020  7:47 am GMT+8 然后放到new Date()中就可以了. 例 ...

  6. JavaScript实时更新中国标准时间

    用到Date对象和BOM中的setInterval()定时器. 效果如下(每隔1s刷新一次) 代码 <!DOCTYPE html> <html lang="en" ...

  7. 将中国标准时间转换成标准格式的代码

    Thu Aug 22 2013 15:12:00 GMT+0800 (中国标准时间)  转换 复制代码 代码如下: function formatTen(num) { return num > ...

  8. 时间字符串转中国标准时间转时间戳转年月日格式

    1.将一个时间转年月日时分秒 (1) 普通字符串时间可转为中国标准时间 let now = new Date("2020-12-12 13:00");  得到:Sat Dec 12 ...

  9. java 标准时间_Java 如何格式化中国标准时间

    String strDate = "Mon Dec 14 2020 13:00:01 GMT+0800 (中国标准时间)"; /** * @Description 中国标准时间转换 ...

最新文章

  1. 通过checkbox选择以逗号拼接删除字符串
  2. Hybris服务器启动日志分析
  3. 【数据库原理及应用】经典题库附答案(14章全)——第十四章:分布式数据库系统
  4. 格子游戏(信息学奥赛一本通-T1347)
  5. mysql 宽容模式_SELinux 宽容模式(permissive) 强制模式(enforcing) 关闭(disabled)
  6. Linux中的pipe(管道)与named pipe(FIFO 命名管道)
  7. spyder ctrl + 鼠标左键点击函数 无法跳转
  8. mysql周德伟课后答案_mysql数据库搜索
  9. 【渝粤教育】国家开放大学2018年春季 0092-22T民法 参考试题
  10. 360linux如何卸载,卸载360安全卫士方法
  11. 【渝粤教育】电大中专计算机常用工具软件 (2)_1作业 题库
  12. 学生考勤及行为管理系统_学生考勤信息管理系统.doc
  13. 一款智能家居APP的雏形
  14. c语言中矩形法求定积分
  15. 2018.06.25 一个不知道叫什么好的U盘启动工具集
  16. matlab:预测股票价格走势
  17. 计算机二级word真题书娟,计算机二级word试题.docx
  18. python如何爬取实时人流量_使用python爬取微信宜出行人流量数据
  19. Riak 简介,第 1 部分: 与语言无关的 HTTP API
  20. 19日病毒提醒:QQ/网银/江湖最凶险!(转)

热门文章

  1. 进化树构建之邻接法(Neighbor-Joining)的介绍
  2. Java画图板界面上的添加
  3. C语言中阶第三篇:循环语句do while透析以及循环语句总结(执行次数、执行特点和循环英文的详解)
  4. Unity 回合制战斗系统(初级篇)
  5. 人工智能的“虚假式繁荣”
  6. esx linux 硬盘 扩容,ESX虚拟机添加新磁盘并扩容逻辑卷
  7. 金蝶K3开发-工业单据自定义控件
  8. table表格自动换行
  9. 淘宝API应用开发小试
  10. Anddroid IM来电铃声通过耳机播放