- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我目前拥有的代码,但我正在努力将其转换为 UTC
var today = Date.UTC(new Date());
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
var H = today.getHours();
var i = today.getMinutes();
var s = today.getSeconds();
if(dd<10) {
dd = '0'+dd
}
if(mm<10) {
mm = '0'+mm
}
today = yyyy + '-' + mm + '-' + dd + ' ' + H + ':' + i + ':' + s;
关于如何让它以相同的时间戳格式工作有什么想法吗?谢谢!
最佳答案
Date
对象始终以 UTC 格式存储 - 您调用的 .getXxx()
函数会将该 UTC 时间隐式转换为您的本地时区。
要提取 UTC 时间的相关字段,您应该使用 .getUTCxxx()
系列函数。
//
// returns the date and time in format 'YYYY-MM-DD hh:mm:ss'
//
// will take a `Date` object, or use the current system
// time if none is supplied
//
function UTC_DateTime(date) {
if (date === undefined) {
date = new Date();
}
function pad2(n) {
return (n < 10) ? ('0' + n) : n;
}
return date.getUTCFullYear() + '-' +
pad2(date.getUTCMonth() + 1) + '-' +
pad2(date.getUTCDay()) + ' ' +
pad2(date.getUTCHours()) + ':' +
pad2(date.getUTCMinutes()) + ':' +
pad2(date.getUTCSeconds());
}
关于javascript - 在 Javascript 日期中使用 UTC 时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51212445/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 3 年前。 Improve th
当我使用 UTC() 函数获取纪元时间(1970 年 1 月 1 日)的总秒数时。但再次使用“new Date(milliseconds)”构造函数将相同的秒数转换为 Date 并没有给出 UTC d
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 11 年前。 Improve thi
我有一个关于 Swift 类型 TimeZone 的问题。我需要一个 UTC0 时区来格式化我的 Date 对象。我正在像这样创建 TimeZone 对象。 let utcTimeZone = Tim
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 5 年前。 Improve t
我遇到了以下代码: datetime.datetime.utcnow().replace(tzinfo=tzutc()) 我看不到 Replace() 调用正在做什么,从阅读文档来看,它似乎将其转换为
我在应用程序中将时区设置为 UTC,如下所示: // set default time zone to UTC TimeZone.setDefault(TimeZone.getTimeZone(Zon
我需要两件事: 将当前时间转换为 UTC(这样我就可以以 UTC 格式存储日期)--> result = java.util.Date. 将加载日期(UTC 格式)转换为任何时区 --> result
我想将日期从本地转换为 UTC,再将 UTC 转换为本地。 例如。 2015 年 4 月 1 日,12:00 PM 然后是 UTC 2015 年 4 月 1 日下午 5:30 对我来说是本地时间,但我
嗨,我有一个长度为几百万的字符向量( rr ),它以 %Y-%m-%d %H:%M:%S 格式表示时间和日期戳。记录在澳大利亚/悉尼。 如何获得代表这一点的 POSIXct 对象(快速)。 我找到了
static String createUTCTime() { Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC")
我正在尝试将语言环境时间转换为 UTC,然后将 UTC 转换为语言环境时间。但我没有得到结果。 public class DateDemo { public static void main(Stri
我正在寻找用纯 Javascript 替换 moment js 功能 - 我需要重新工作的项目将来不会有 moment.js 可用。我已经有一段时间没有使用 javascript Date 了,所以需
我想获取与 UTC 相关的用户时区,然后将其显示为 UTC +/- 。例如,加利福尼亚用户应显示 UTC -8(或 -7,视情况而定),而巴林用户应显示 UTC +3,等等。 下面的代码并没有告诉我它
我正在尝试将毫秒转换为 UTC 日期对象,如下所示 - var tempDate = new Date(1465171200000); // --> tempDate = Mon Jun 06 201
我一直很困惑为什么下面的代码会导致我的日期从 25 日更改为 24 日 SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy"); DateTi
我需要将字符串转换为 UTC 日期,然后将 UTC 日期转换为本地日期。 这是我的代码: var dateStr = "9/8/2015 12:44:00 PM"; console.log(strto
我正在用 PHP 编写一个 Twitter 网络服务。当用户登录时,我收到此节点: -18000 我必须更改脚本的时区,以便它适应用户的实际时区。我为此找到的唯一 php 函数是: date_defa
在 PHP 文档中,list of supported time zones , UTC 被列出两次: UTC 等/UTC 这两者之间有什么概念上的区别,还是它们只是同义词? 最佳答案 首先回答问题:
在 PHP 文档中,list of supported time zones , UTC 被列出两次: UTC 等/UTC 这两者之间有什么概念上的区别,还是它们只是同义词? 最佳答案 首先回答问题:
我是一名优秀的程序员,十分优秀!