两个日期之间的差值计算

两个日期之间的差值计算

近期在写关于计算时间之间的业务处理,当中的各种差值语法初六汇总

1.前端(以JQUERY为例)

语法:核心:采用时间戳的相减方式

//以当前时间减去制定日期计算相差的时间
var  currentTime=new Date();
function  calculateIntervalTime(currentTime,appointTime)
{
    //上篇文章已经介绍:getTime()---
    //--代表时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。
	var  currentT=new Date(currentTime).getTime();
	var  appointT=new Date(appointTime).getTime();
	//两个时间戳差值(精确到毫秒数)
	var  intervalTime=currentT-appointT;
    //1000 * 60 * 60 * 24--表示的是把差的毫秒数转换为天数
    //60 * 60 * 1000       --时
    //60 * 1000            --分
    //计算天,时,分(差值)
    var intervalDays=Math.Double(intervalTime/24*3600*1000);
    //计算时
    var intervalHour=Math.Double(intervalTime/3600*1000);
    //计算分
     var intervalMinutes=Math.Double(intervalTime/60*1000);
    //返回你想返回的数据(比如返回小时)
    var destinationDate=intervalHour;
    return destinationDate;
    

}

2.后端

2.1 以java处理

核心代码

public String timeSubtract(String t1,String t2)
{
    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //和上面的JS处理方式差不多
    long compareT1=simpleDateFormat.parse(t1).getTime();
    long compareT2=simpleDateFormat.parse(t2).getTime();
    long subtractResult=compareT1-compareT2;
    //计算天
    long intervalDays=subtractResult/1000*24*60*60;
    //计算时
    long  intervalMin=subtractResult/1000*60*60;
    //计算分
    long  intervalMin=subtractResult/1000*60;
    
    
}

设置时区

formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));

最近看到的一篇关于JAVA的处理时间差:写的挺全的

原理其实很简单,就是根据两个日期相差的天数,小时数,分钟数,秒数计算之后转换成HH:mm格式,废话不多说,直接上代码

public static String timeSubtraction(String time1, String time2) throws ParseException {
/**

*@description time2 是大的时间

*@param [time1, time2]

*@return java.lang.String

*/

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小时制

long newTime1 = simpleDateFormat.parse(time2).getTime();

long newTime2 = simpleDateFormat.parse(time1).getTime();

Long result = newTime1 - newTime2; //获取两时间相差的毫秒数

long nd = 1000 * 24 * 60 * 60;

long nh = 1000 * 60 * 60;

long nm = 1000 * 60;

long hour = result % nd / nh; //获取相差的小时数

long min = result % nd % nh / nm; //获取相差的分钟数

long day = result / nd;

SimpleDateFormat formatter = new SimpleDateFormat("HH:mm");//初始化Formatter的转换格式。

long hMiles = hour * 3600000; //小时数转换成毫秒

long mMiles = min * 60000; //分钟数转换成毫秒

long resulMiles = (hMiles + mMiles);

//下面这段很重要 ,计算之后设置时区,不然会差几小时

formatter.setTimeZone(TimeZone.getTimeZone("GMT+00:00"));

String resultFormat = formatter.format(resulMiles);

//我这段是在一天内计算的 如果大于一天 就把下面的 day*24加到小时上就可以了

return resultFormat + "," + day;

}
版权声明:本文为CSDN博主「阿里巴巴达摩院」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_32009809/article/details/114111375

2.2以C#的处理

核心代码(计算差值天数)

//当前时间 
 DateTime currentTime = DateTime.Now;
//要比较的时间
//compareTime为你传入的要比较的日期时间(防止传入的字符串格式)
//例如:2021年12月23日15:30:21
 DateTime compareTime = Convert.ToDateTime(compareTime);
//TimeSpan:表示的是一个时间间隔
//计算差值
 TimeSpan intervalHuors = currentTime.Subtract(scanTime);
//转化为差值对应的小时数
 double intervalHToDouble = intervalHuors.TotalHours;

说明 TimeSpan的函数的用法:

TimeSpan(Int64)将 TimeSpan结构的新实例初始化为指定的刻度数。

(DateTime.Tick:是计算机的一个计时周期,单位是一百纳秒,即一千万分之一秒)

TimeSpan(Int32, Int32, Int32)将 TimeSpan结构的新实例初始化为指定的小时数、分钟数和秒数。

TimeSpan(Int32, Int32, Int32, Int32)将 TimeSpan结构的新实例初始化为指定的天数、小时数、分钟数和秒数。

TimeSpan(Int32,Int32, Int32, Int32, Int32)将新的 TimeSpan结构初始化为指定的天数、小时数、分钟数、秒数和毫秒数。
//这个以自己的查询数据库获去时间军饷判断记录下(自己学习使用)
public Boolean searchTimeMlotId(string mlotid)
        {
            if (!mlotid.Equals(null))
            {
                try
                {
                    string str = System.Configuration.ConfigurationManager.AppSettings["sqlConnectionString"];
                    SqlConnection conn = new SqlConnection(str);//数据库入库开始
                    DataSet Dstype = new DataSet();
                    string Sqltype = "select  top 1   scan_time,scan_result   from scanlog where MLOTID='" + mlotid + "' order by scan_time";
                    /*SqlDataAdapter Datype = new SqlDataAdapter(Sqltype, conn);
                    Datype.Fill(Dstype);
                    DataTable table = new DataTable();
                    table = Dstype.Tables[0];*/

                    using (SqlCommand cmd=new SqlCommand(Sqltype,conn))
                    {
                        if (conn.State==ConnectionState.Closed)//判断是否处于关闭状态
                        {
                            conn.Open();//打开数据库连接
                        }
                        SqlDataReader read = cmd.ExecuteReader();//用cmd点上ExecuteReader()方法,方法类型属于SQLDataReader
                        int i = 1;
                        if (read.Read()==true)
                        {
                            //
                            string scan_time = read["scan_time"].ToString();
                            string scan_result = read["scan_result"].ToString();

                            if (scan_result.Contains("异常"))
                            {
                                return true;
                            }
                            DateTime currentTime = DateTime.Now;
                            DateTime scanTime = Convert.ToDateTime(scan_time);
                            TimeSpan intervalHuors = currentTime.Subtract(scanTime);
                            double intervalHToDouble = intervalHuors.TotalHours;
                            string script = "<script>alert('" + mlotid + "此码12小时内扫过,请更换扫码!," + "')</script>";
                            ClientScript.RegisterStartupScript(GetType(), "", script);
                            /* int aa=new DateTime().GetHashCode();*/
                            return intervalHToDouble < 12.0?true:false;
                        }
                        else
                        {

                            string script = "<script>alert('" + mlotid + "近期已经通过扫码验证,请更换其他资材LOTID" + "')</script>";
                            ClientScript.RegisterStartupScript(GetType(), "", script);
                            return false;
                        }
                    }


                    return false;
                   
                }
                catch (Exception)
                {

                    throw;
                }
            }
            else
            {
                return false;
            }
        }

完结撒花

img
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值