- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在处理日期并实现了这两个命令 Date(d.getTime())
和new Date(d.getTime())
是不同的。
当我运行此代码片段时:
var d = new Date(2016,11,12);
console.log(Date(d.getTime()));
console.log(new Date(d.getTime()));
我有这个结果:
(index):68 Thu Dec 12 2019 18:02:41 GMT-0300 (Horário Padrão de Brasília)
(index):69 Mon Dec 12 2016 00:00:00 GMT-0200 (Horário de Verão de Brasília)
为什么它们不同?
我试图寻找一些答案,但没有找到。这些是我浏览过的一些引用资料:
Difference between Date(dateString) and new Date(dateString)
Why we can't call methods of Date() class without new operator
最佳答案
Date
获取 window
/global Date
对象,new Date()
创建一个新的静态 日期
对象。当您创建一个new Date()
时,您会及时卡住它的值。
当您使用Date()
时,它会返回当前日期的字符串表示形式,就像调用new Date().toString()
一样。
根据您的代码:
// this is a new date, Dec. 12, 2016
var d = new Date(2016,11,12);
// this returns the time value of Date d
d.getTime()
// Calling Date as a function just returns a string for
// the current date, arguments are ignored
console.log(Date(d.getTime()));
// this creates a new date based on d's time value
console.log(new Date(d.getTime()));
关于javascript - JavaScript 中 Date(d.getTime()) 和 new Date(d.getTime()) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59312685/
假设我有一个日历对象 c。这行的结果是什么: c.getTimeInMilis(); 和 c.getTime().getTime(); 最佳答案 表面上它们是相同的。 但我更喜欢第一个,因为它不会(明
虚拟机的运行时间是如何计算的,而系统时间是如何计算的?我知道我可以通过调用来获取 AVM 的当前运行时间: getTimer(); 我可以通过执行以下操作来获取当前的 unix 系统时间: new D
对于负毫秒值,Java 和 JavaScript 似乎给出了不同的结果。 Java: System.out.println(new Date(-12220000000000L)); Wed Sep 2
我目前正在验证生成的 Excel 工作表是否包含正确呈现的“时间戳”,其日期和时间部分位于不同的单元格中,并且其格式已根据语言环境进行了调整。 我的测试用例失败了,因为当我从字符串中读回 Excel
这个问题已经有答案了: Difference between Date(dateString) and new Date(dateString) (10 个回答) 已关闭 3 年前。 我一直在处理日期
是否可以创建一个 jQuery 函数来获取当前日期和时间?我一直在查看文档,但到目前为止还没有找到任何东西...... 最佳答案 @nickf 是正确的。然而,更准确地说: // if you try
当我尝试设置每天的倒计时时,遇到此错误。这是我的脚本: var date; var display = document.getElementById('time'); $(document).rea
所以我在比较两个日期时遇到问题。今天存储为 2016-04-17,日期存储为相同的值,只是日期后面有分钟和秒。即使我已将今天存储为日期,today.getTime() 似乎也不起作用。感谢您的帮助。
我有下面的代码: Date now = CalendarUtils.getCalendar(getDefaultTimeZone()) .getTime(); Cale
这个问题已经有答案了: Java Calendar adds a random number of milliseconds? (4 个回答) 已关闭 5 年前。 我想知道为什么我总是得到这个日期之间
我遗漏了一些东西,但我在网上找不到任何提示。当我使用 Javascript 的 getTime() 函数时,它似乎不是从 1970, 01, 01, 0, 0, 0, 0 即 1970 年午夜算起,而
谁能给我解释一下这个例子,请: http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_gettime function myFuncti
我对 HTML 和 Javascript 相当陌生,所以我正在尝试制作一个小的增量游戏作为练习。这是我试图用来计算自动增益/秒的代码,以及在选项卡未处于焦点和 setInterval 停止运行时进行相
我正在用 html 做一个小测验,有 10 个问题,每个问题都在一个单独的 html 页面中。当用户按下 index.html 中的开始按钮时,我执行以下操作: var s = new Date();
我知道有很多与此相关的问题,但我找不到有帮助的信息...我只需要在我的脚本中解决这个问题,有人可以帮助我吗? function dateValidation() { this.validateDate
我在尝试设置一个计数器来计算填写特定表格所花费的总时间时遇到此错误。尝试了以下操作, //script var timeStart; var timeStop; $(document).load(fu
我在我的系统中使用时间戳。一切正常,但我发现将 [12:2:2] 作为时间戳很烦人。因为您不知道它是 20 还是 2。 我又累又病,开始以为是编译器的问题。 java.util.Date da
有没有办法在不获取毫秒时间的情况下使用 Date().getTime() 函数?如果没有,是否有 .getTime() 的替代品,它只会给我以分钟为单位的精度? 我也不确定如何从日期对象中去除毫秒数。
我想传递日期对象方法,例如 getMinutes()、getTime() 或 getDay() 作为回调功能如: formatDate = (date, callback) => { date
我使用 javascript getTime() 函数将出生日期存储在 mongodb 中。现在我想在我的数据库中查询所有 20 到 30 岁之间的人。我如何在服务器上执行此操作? { birth_d
我是一名优秀的程序员,十分优秀!