当前位置: 首页 > 知识库问答 >
问题:

Javascript日期差异变量n

宗政卓
2023-03-14
<!DOCTYPE html>

<html>
<head>
<title></title>
<meta name="author" content="" />
<meta name="keywords" content="" />

<style type="text/css">

body {
background-color: rgb(250, 100, 100); }

#pc {
width: 500px;
height:300px;
background-color: rgb(247, 240, 224);
border: rgb(50, 153, 254) 5px groove;
margin-left: auto;
margin-right: auto;
margin-top: 100px; }

#clock {
background-color: rgb(50, 153, 254);
float: left;
border: rgb(50, 153, 254) 5px groove;
height: 150px;
width: 150px;
margin-top: 45px;
margin-left: 25px; }

#HT1 {
background-color: black;
color: yellow;
text-align: center;
margin-top: -1px;
margin-left: auto;
margin-right: auto;
width: 100px;
}

#HT2 {
background-color: black;
color: yellow;
text-align: center;
margin-top: -1px;
width: 125px;
margin-left: auto;
margin-right: auto;
}

#Date {
background-color: black;
color: yellow;
font-size: 18px;
text-align: center;
width: 125px;
margin-top: -12px;
margin-left: 15px;
}

#Time {
background-color: black;
color: yellow;
font-size: 18px;
text-align: center;
width: 125px;
margin-top: 50px;
margin-left: 15px;
}

#hourglass {
position: absolute;
margin-left: 43px;
}

#countdown {
background-color: rgb(50, 153, 254);
float: right;
border: rgb(50, 153, 254) 5px groove;
height: 200px;
width: 250px;
margin-top: 25px;
margin-right: 25px; }

#title {
text-align: center; 
margin-bottom: 0px; 
font-variant: small-caps;
color: yellow; 
background-color: black; 
width: 100%; 
margin-top: 0px;
border-bottom: rgb(50, 153, 254) 5px groove; 
height: 45px;
}       

#day {
background-color: black;
color: yellow;
font-size: 18px;
text-align: right;
width: 125px;
}

#hours {
background-color: black;
color: yellow;
font-size: 18px;
text-align: right;
width: 125px;
}

#minutes {
background-color: black;
color: yellow;
font-size: 18px;
text-align: right;
width: 125px;
}

#seconds {
background-color: black;
color: yellow;
font-size: 18px;
text-align: right;
width: 125px;
}
</style>

<script type="text/JavaScript" src="main.js">
</script>
</head>

<body onload="day(); current(); DateDiff();" >
<div id="pc">

<h1 id="title">Count Down to <span id="Event">_ _ _ _ _ _ _ _</span></h1>

<div id="clock">
<h3 id="HT1">Time</h3>

<div id="Date">
<span id=Month></span>/<span id="Day"></span>/<span id="Year"></span>
</div>

<img id="hourglass" src="clock.png" height="64px" width="64px" alt="This is an hourglass" />
<span id="Date" ></span>

<div id="Time">
<span id="Hours"></span>
</div>

</div>

<div id="countdown">
    <h3 id="HT2">Countdown</h3>
    <div id="day"><span id="ED">____</span> Days</div><br />
    <div id="hours"><span id="EH">____</span> Hours</div><br />
    <div id="minutes"><span id="EM">____</span> Minutes</div><br />
    <div id="seconds"><span id="ES">____</span> Seconds</div><br />
</div>



</div>
</body>

</html>

在这个项目中,我试图弄清楚两个日期之间的差异有多长。一个日期是当前日期,另一个是一个人选择的日期。我遇到的问题是,由于变量中的日期是字符串,而不是数字,所以我得到了nan。我尝试使用parseInt()属性,但是,这给了我8个值,这并不是now和New Years eve之间的区别。那么,我如何将一个变量更改为一个数字,但要保持它的日期格式呢?

这是我当前的代码,既用于查找未来日期,也用于查找差异。

/* Future Date Variables */
var DayF;
var MonthF;
var FullF;
var event;
var time;
var someday;

/* Asks for event name and the date */
function day()
{
    event = prompt("Please input a day you want to count down too");
    while (event == 0 || event == null)
    {
        if (event == 0 || event == null)
        {
            alert("That is not a valid event, please try again");
            event = prompt("Please input a day you want to count down too");
        }
    }
    document.getElementById("Event").innerHTML = event;
    
    MonthF = prompt("Please input the month the event is in");
    DayF = prompt("Please input the day the event is on");
    FullF = prompt("Please input the year, note we do not go into the past");
    
    if(MonthF < 1 || Month > 12)
    	{
    		alert("There are only 12 months, please try again");
    		MonthF = prompt("Please input the month the event is in");
    	}
    	
    if(DayF > 31 || DayF < 1)
    	{
    		alert("There is only an average of 31 days in a month");
    		DayF = prompt("Please input the day the event is on");
    	}
    	
    if(FullF < 2018)
    	{
    		alert("I said we do not go into the past");
    		FullF = prompt("Please input the year, note we do not go into the past");
    	}
    someday =(MonthF + "/" + DayF + "/" + FullF);
}

/* Current Time and Date Variables */
var nd = new Date();
var M = nd.getMonth();
var D = nd.getDate();
var F = nd.getFullYear();
var H = nd.getHours();
var Min = nd.getMinutes();
var Sec = nd.getSeconds();
var ap = "am";

/* Gets and Displays current time */
function current()
{
	M = M + 1;

	document.getElementById("Month").innerHTML= M;
	document.getElementById("Day").innerHTML= D;
	document.getElementById("Year").innerHTML= F;
	
	if (H > 12) 
	{
    H = H - 12;
    ap = "pm";

        if (H > 12) 
        {
        	H = H - 12;
            ap = "am";
    	}

        if (H == 0) 
        {
            H = 12;
        }

        if (Min < 10) 
        {
            Min = "0" + Min;
        }
    }   
    document.getElementById("Hours").innerHTML= + H + ":" + Min + " " + ap;	   
}

var newdate 

function DateDiff()
{
	newdate = (M + "/" + D + "/" + F);
	var diffDays = parseInt((someday - newdate) / (1000 * 60 * 60 * 24)); 
	alert(diffDays);

}

共有1个答案

山越
2023-03-14

本质上,您需要将日期转换为以毫秒为标记的int。那就找出区别。除以一天中的毫秒。并将该数字舍入到最接近的int。

你可以这样做:

var date1 = "2018-04-19T20:23:01.804Z";
var date2 = "2018-12-31T23:59:34.527Z"; //New Year's Eve

var dateDifference;

function getDifference(dateA, dateB){
  var dateInt1 = new Date(date1);
  var dateInt2 = new Date(date2);
  if(dateInt1<dateInt2){
    dateDifference = Math.round((dateInt2 - dateInt1)/86400000);
  }else{
    dateDifference = Math.round((dateInt1 - dateInt2)/86400000);
  }
}

getDifference(date1, date2);

console.log(dateDifference);
 类似资料:
  • 问题内容: 如何找到两个日期之间的差异? 问题答案: 通过使用Date对象及其毫秒值,可以计算出差异: 您可以通过将毫秒除以1000来将秒转换为秒,然后将结果转换为整数来获得秒数(作为整数/整数):(除去代表毫秒的小数部分): 然后可以通过除以60并将其转换为整数,然后除以60并将其转换为整数,然后以相同的方式获得更长的时间单位来获得整数。由此,可以创建一个功能,以时间单位的最大值获得一个较低的单

  • 问题内容: 我有一个PostgreSQL函数来计算日期差: 如果直接减去日期,则会计算出差额。但是在我的情况下,日期在变量as和中存在,这导致了问题。 如何减去变量中包含的日期? 问题答案: 除错 你的函数是做可以做 很多 简单。语法错误的实际原因在这里: 看来您正在尝试转换为,这是毫无意义的,因为您的参数已声明为已经。 这也行不通。我在这里引用手册: 为避免语法歧义,“字符串”类型的语法只能用于

  • 我正在尝试使用QueryDSL计算用户年龄和日期差。 但失败时出现此错误 谢谢

  • 目前我正在尝试将日期计算从javascript转换为java,因为我想将它用于Android开发。所以在javascript中,我使用了方法,它将日期转换为自1970年1月1日以来经过的毫秒。在java中,具有相同功能的方法称为。在java和javascript留档中,描述完全相同,但当我插入相同的日期时,我得到了两个完全不同的值。例如: Java代码 Javascript 所以我的问题是: 为什

  • 我已经搜索了4个小时了,但是还没有找到一个用JavaScript计算两个日期(年、月和天)之间差异的解决方案,比如:2010年4月10日是3年、x月和y天之前。 有很多解决方案,但它们只提供了日、月或年的格式差异,或者它们不正确(意味着没有考虑一个月或闰年的实际天数,等等)。做那件事真的有那么难吗? 我看了一下: http://momentjs.com/->只能输出年、月或天的差异 http://

  • 问题内容: 我正在使用以下代码获取当前日期(格式为12/31/1999,即mm / dd / yyyy): 我还有另一个日期格式为:2019-08-25(即yyyy / mm / dd), 所以我想找出日期之间的天数差异,如何找到天数上的差异? (换句话说,我想找到当前日期-yyyy / mm / dd格式化日期之间的差异) 问题答案: 并不是真正可靠的方法,最好使用JodaTime 这是一个近似