使用 'DD/MM/YYYY HH:MM:SS' 获取日期和格式之间的差异
Get difference between dates and format with 'DD/MM/YYYY HH:MM:SS'
如何将毫秒从 date.now()
转换为格式 DD/MM/YYYY HH:MM:SS
或将带有日期的字符串转换为毫秒。
我还有一个字符串 returns 以下内容:
appointment: "21/11/2019 18:00:30"
我需要在date.now和约会[=25]之间获得时间差 =]日期
您需要将日期字符串转换为这样的有效格式 "2019-11-21T18:00:30";
,然后使用 .getTime()
获取差异
这样试试:
let difference = Math.abs(new Date().getTime() - new Date(this.appointment).getTime());
var seconds = difference / 1000;
var minutes = difference / 1000 / 60;
var hours = minutes / 60;
console.log("Difference in Hours",hours)
console.log("Difference in minutes",minutes)
console.log("Difference in seconds",seconds)
var d = new Date,
dformat = [d.getDate(),
d.getMonth()+1 ,
d.getFullYear()].join('/')+' '+
[d.getHours(),
d.getMinutes(),
d.getSeconds()].join(':');
console.log(dformat)
输出
26/12/2019 15:48:17
要转换日期,您可以使用 DatePipe:
let datePipe = new DatePipe();
datePipe.transform(date, 'dd/MM/yyyy hh:mm:ss');
要了解 Date.now() 和日期之间的区别,您可以看一下这个 ,或者您可以使用 moment 之类的那:
import * as moment from 'moment';
// ...
const difference = moment(moment(),"DD/MM/YYYY HH:mm:ss").diff(moment(appointment,"DD/MM/YYYY HH:mm:ss"))
您需要像这样安装 moment
:
npm install --save moment
时差:
const dateAppointment = new Date('1/11/2019 18:00:30');
const currentDate = new Date();
console.log(`currentDate is `, currentDate );
const diffTime = Math.abs(currentDate - dateAppointment);
const diffHours = Math.ceil(diffTime / (1000 * 60 * 60));
console.log(`diffHours is`, diffHours );
一个例子:
const dateAppointment = new Date('1/11/2019 18:00:30');
const currentDate = new Date();
console.log(`currentDate is `, currentDate );
const diffTime = Math.abs(currentDate - dateAppointment);
const diffHours = Math.ceil(diffTime / (1000 * 60 * 60));
console.log(`diffHours is`, diffHours );
更新:
HTML:
<p>currentDate is {{ currentDate | date : 'dd.MM.y, HH:mm:ss' }} </p>
<p>dateAppointment is {{ dateAppointment | date : 'dd.MM.y, HH:mm:ss' }} </p>
<p>differenceHour is {{ differenceHour }} </p>
打字稿:
name = 'Angular';
currentDate = new Date();
dateAppointment = new Date('1/11/2019 18:00:30');
differenceHour = 0;
constructor(){
const diffTime = Math.abs(this.currentDate - this.dateAppointment);
this.differenceHour = Math.ceil(diffTime / (1000 * 60 * 60));
}
有一种方法可以在没有第 3 方的情况下做到这一点
let date = new Date(Date.now());
date = `${date.getDate()}:${date.getMonth() + 1}:${date.getFullYear()}:${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
还有一种方法是使用angular,如果你使用Angular,你可以使用"DatePipe",使用转换方法,像这样:
datePipe.transform(date, 'dd/MMM/yyyy hh:mm:ss');
顺便说一句,要获得日期差异,只有一种方法
const currentDate = new Date('26/12/2019');
const appoinemnt = new Date('15/1/2020');
const diffTime = Math.abs(appoinemnt - currentDate );
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
console.log(diffDays);
如何将毫秒从 date.now()
转换为格式 DD/MM/YYYY HH:MM:SS
或将带有日期的字符串转换为毫秒。
我还有一个字符串 returns 以下内容:
appointment: "21/11/2019 18:00:30"
我需要在date.now和约会[=25]之间获得时间差 =]日期
您需要将日期字符串转换为这样的有效格式 "2019-11-21T18:00:30";
,然后使用 .getTime()
这样试试:
let difference = Math.abs(new Date().getTime() - new Date(this.appointment).getTime());
var seconds = difference / 1000;
var minutes = difference / 1000 / 60;
var hours = minutes / 60;
console.log("Difference in Hours",hours)
console.log("Difference in minutes",minutes)
console.log("Difference in seconds",seconds)
var d = new Date,
dformat = [d.getDate(),
d.getMonth()+1 ,
d.getFullYear()].join('/')+' '+
[d.getHours(),
d.getMinutes(),
d.getSeconds()].join(':');
console.log(dformat)
输出
26/12/2019 15:48:17
要转换日期,您可以使用 DatePipe:
let datePipe = new DatePipe();
datePipe.transform(date, 'dd/MM/yyyy hh:mm:ss');
要了解 Date.now() 和日期之间的区别,您可以看一下这个
import * as moment from 'moment';
// ...
const difference = moment(moment(),"DD/MM/YYYY HH:mm:ss").diff(moment(appointment,"DD/MM/YYYY HH:mm:ss"))
您需要像这样安装 moment
:
npm install --save moment
时差:
const dateAppointment = new Date('1/11/2019 18:00:30');
const currentDate = new Date();
console.log(`currentDate is `, currentDate );
const diffTime = Math.abs(currentDate - dateAppointment);
const diffHours = Math.ceil(diffTime / (1000 * 60 * 60));
console.log(`diffHours is`, diffHours );
一个例子:
const dateAppointment = new Date('1/11/2019 18:00:30');
const currentDate = new Date();
console.log(`currentDate is `, currentDate );
const diffTime = Math.abs(currentDate - dateAppointment);
const diffHours = Math.ceil(diffTime / (1000 * 60 * 60));
console.log(`diffHours is`, diffHours );
更新:
HTML:
<p>currentDate is {{ currentDate | date : 'dd.MM.y, HH:mm:ss' }} </p>
<p>dateAppointment is {{ dateAppointment | date : 'dd.MM.y, HH:mm:ss' }} </p>
<p>differenceHour is {{ differenceHour }} </p>
打字稿:
name = 'Angular';
currentDate = new Date();
dateAppointment = new Date('1/11/2019 18:00:30');
differenceHour = 0;
constructor(){
const diffTime = Math.abs(this.currentDate - this.dateAppointment);
this.differenceHour = Math.ceil(diffTime / (1000 * 60 * 60));
}
有一种方法可以在没有第 3 方的情况下做到这一点
let date = new Date(Date.now());
date = `${date.getDate()}:${date.getMonth() + 1}:${date.getFullYear()}:${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
还有一种方法是使用angular,如果你使用Angular,你可以使用"DatePipe",使用转换方法,像这样:
datePipe.transform(date, 'dd/MMM/yyyy hh:mm:ss');
顺便说一句,要获得日期差异,只有一种方法
const currentDate = new Date('26/12/2019');
const appoinemnt = new Date('15/1/2020');
const diffTime = Math.abs(appoinemnt - currentDate );
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
console.log(diffDays);