【问题标题】:How to get EPOCH date time format in angular 6?如何以角度 6 获取 EPOCH 日期时间格式?
【发布时间】:2021-03-02 13:08:41
【问题描述】:
我希望我的日期、时间以角度为 EPOCH 格式。谁能帮我获取 EPOCH 格式的日期?
let UCCdateformated = formatDate(today, 'yyyy-MM-dd', 'en-US', '+0530');
console.log("UCCdateformated ",UCCdateformated);
【问题讨论】:
标签:
node.js
angular
angular6
【解决方案1】:
您可以在 Vanilla JavaScript 中做到这一点,而无需依赖任何 Angular 的管道。
要获取当前时刻的纪元时间,
const epochNow = (new Date).getTime();
console.log(epochNow);
要获取特定小时的纪元时间(例如,今天的日期凌晨 12 点),
const epochToday = (new Date).setHours(0,0,0,0).getTime();
console.log(epochToday);
【解决方案2】:
您可以使用datePipe,如下所示。但是角度日期不附带语言环境,因此您可以传递一些占位符,例如====,然后将其替换为locale
formatDate(date = Date.now(), format=`yyyy-MM-dd ==== z`, locale='en-US'){
let dateStr = this.datePipe.transform(date, format);
let dateStrWithLocale = dateStr.replace('====', locale);
alert(dateStrWithLocale);
}
stackblitz
【解决方案3】:
使用角度 11
const epochNow = (new Date).getTime();
console.log(epochNow); get the time in epoch assuming that is in milliseconds
如果要设置默认时间:
示例:
const epochNow = (new Date).setHours(0,0,0,0);
console.log(epochNow); get the time 12am with your time zone
现在,如果设置了这些属性中的任何一个,您可以更改结果