New Date() 在不同的地方给出不同的响应
New Date() giving different response in different places
如果这个问题看起来很愚蠢,真的很抱歉。
所以我去Typescript Playground写了一个简单的获取时间代码。
代码:
// 1 Week Logic
function get1WeekDate() {
const now = new Date();
return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
}
// 2 Week Logic
function get2WeeksDate() {
const now = new Date();
return new Date(now.getTime() - 14 * 24 * 60 * 60 * 1000);
}
console.log("Current Date:",new Date());
console.log("Last Week Date:",get1WeekDate());
console.log("Last 2 Week Date:",get2WeeksDate());
输出:
[LOG]: "Current Date:", Date: "2022-05-30T13:19:06.403Z"
[LOG]: "Last Week Date:", Date: "2022-05-23T13:19:06.405Z"
[LOG]: "Last 2 Week Date:", Date: "2022-05-16T13:19:06.406Z"
但是 jsfiddle 中的相同代码给了我不同的结果:
Jsfiddle 输出:
"Current Date:", [object Date] { ... }
"Last Week Date:", [object Date] { ... }
当我在 angular 组件中添加代码并在浏览器 DOM 中使用 console.log 检查结果时
输出:
我想在时间戳中获取今天的日期以及 1 周前和 1 个月前的日期。我怎样才能在打字稿中做到这一点
我想要像“2022-05-30T12:30:51.272Z”这样的日期
--编辑--
正如@adiga 正确指出的那样:
currentdate.toISOString();
如果你想要更详细的方法:
前段时间自己搜索了一下好像没有,在Whosebug的帮助下终于找到了这个功能:
// Get a proper formatted timestamp.
static getTimeStamp(): string {
let currentdate = new Date();
let timestamp = currentdate.getUTCFullYear() + "-" +
("0" + (currentdate.getUTCMonth() + 1)).slice(-2) + "-" +
("0" + currentdate.getUTCDate()).slice(-2) + "T" +
("0" + currentdate.getUTCHours()).slice(-2) + ":" +
("0" + currentdate.getUTCMinutes()).slice(-2) + ":" +
("0" + currentdate.getUTCSeconds()).slice(-2) + "." +
("000000" + currentdate.getMilliseconds()).slice(-6) + "Z";
console.log("Generated TimeStamp",timestamp);
return timestamp;
}
在您的情况下,您必须将毫秒限制为 3 位数。
如果这个问题看起来很愚蠢,真的很抱歉。 所以我去Typescript Playground写了一个简单的获取时间代码。
代码:
// 1 Week Logic
function get1WeekDate() {
const now = new Date();
return new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000);
}
// 2 Week Logic
function get2WeeksDate() {
const now = new Date();
return new Date(now.getTime() - 14 * 24 * 60 * 60 * 1000);
}
console.log("Current Date:",new Date());
console.log("Last Week Date:",get1WeekDate());
console.log("Last 2 Week Date:",get2WeeksDate());
输出:
[LOG]: "Current Date:", Date: "2022-05-30T13:19:06.403Z"
[LOG]: "Last Week Date:", Date: "2022-05-23T13:19:06.405Z"
[LOG]: "Last 2 Week Date:", Date: "2022-05-16T13:19:06.406Z"
但是 jsfiddle 中的相同代码给了我不同的结果: Jsfiddle 输出:
"Current Date:", [object Date] { ... }
"Last Week Date:", [object Date] { ... }
当我在 angular 组件中添加代码并在浏览器 DOM 中使用 console.log 检查结果时 输出:
我想在时间戳中获取今天的日期以及 1 周前和 1 个月前的日期。我怎样才能在打字稿中做到这一点 我想要像“2022-05-30T12:30:51.272Z”这样的日期
--编辑--
正如@adiga 正确指出的那样:
currentdate.toISOString();
如果你想要更详细的方法:
前段时间自己搜索了一下好像没有,在Whosebug的帮助下终于找到了这个功能:
// Get a proper formatted timestamp.
static getTimeStamp(): string {
let currentdate = new Date();
let timestamp = currentdate.getUTCFullYear() + "-" +
("0" + (currentdate.getUTCMonth() + 1)).slice(-2) + "-" +
("0" + currentdate.getUTCDate()).slice(-2) + "T" +
("0" + currentdate.getUTCHours()).slice(-2) + ":" +
("0" + currentdate.getUTCMinutes()).slice(-2) + ":" +
("0" + currentdate.getUTCSeconds()).slice(-2) + "." +
("000000" + currentdate.getMilliseconds()).slice(-6) + "Z";
console.log("Generated TimeStamp",timestamp);
return timestamp;
}
在您的情况下,您必须将毫秒限制为 3 位数。