SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
Date d1 = df.parse("2004-01-02 11:30:24");
Date d2 = df.parse("2004-03-26 13:31:40");
long diff = d2.getTime() - d1.getTime();
long days = diff / (1000 * 60 * 60 * 24);
diff = diff - days * (1000 * 60 * 60 * 24);
long hours = diff / (1000 * 60 * 60);
diff = diff - hours * (1000 * 60 * 60);
long minutes = diff / (1000 * 60);
diff = diff - minutes * (1000 * 60);
long seconds = diff / 1000;
System.out.println("" + days + "天" + hours + "时" + minutes + "分" + seconds + "秒");
}catch (Exception e){
}
js日期相减
//like12 add,20220822,增加时间限制 防止误操作
var reservationsDate = arg.data.get("reservationsDate");
var nowDate = new Date();
//js时间比较
var days = parseInt((nowDate - reservationsDate)/ (1000 * 60 * 60 * 24));//核心:时间戳相减,然后除以天数
if(days > 5){
dorado.MessageBox.alert("(" + days + ")天前的预约,已超5天,禁止操作!");
return;
}
js秒转时分秒
//js秒转时分秒(js除法结果是小数 需自己取整)
var hours = parseInt(value / 3600);
var minutes = parseInt(value % 3600 / 60);
var seconds = value % 60;
var value = "";
if(hours > 0){
value += hours + "时";
}
if(minutes > 0){
value += minutes + "分";
}
value += seconds + "秒";