JS-时区选择-获取当前时区

一个时区选择插件的使用,以及获取当前时区的例子。

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
		<script src="timezones.full.js"></script>
		<script src="moment-timezone-with-data-2012-2022.min.js"></script>
		<style>
			div {
				width: 500px;
				margin: 0 auto;
				margin-top: 400px;
			}
		</style>
	</head>

	<body>
		<div>
			<select></select>
			<br /><br />
			<button>获取值</button>
			<br /><br />
			<span></span>
		</div>
	</body>
	<script>
		//初始化
		$('select').timezones();
//		$('select').val('Asia/Shanghai');
		//选中当前时区,有些浏览器不支持
		$('select').val(Intl.DateTimeFormat().resolvedOptions().timeZone);
		//使用moment-timezone获取当前时区
//		$('select').val(moment.tz.guess());
		$("button").click(function() {
			$("span").html($("select").val());
		});
		
//		console.log(Intl.DateTimeFormat().resolvedOptions().timeZone)
		//推测当前时区
		console.log(moment.tz.guess());
		
		//方法可返回格林威治时间和本地时间之间的时差,以分钟为单位。
		var d = new Date();
		var n = d.getTimezoneOffset();
	</script>

</html>

效果:
在这里插入图片描述
Intl是Internationalization API,但是有些浏览器不支持。

Intl.DateTimeFormat().resolvedOptions().timeZone

Intl浏览器支持情况:
在这里插入图片描述
参考

https://caniuse.com/#search=DateTimeFormat
https://github.com/firstandthird/timezones
http://momentjs.com/timezone/

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值