使用 javascript 或 datejs 比较两个日期(日期差异)
我正在尝试比较芬兰时间形式的两个日期,如下所示:dd.mm.YYYY 或 dmYYYY 或 dd.m.YYYY 或 d.mm.YYYY。
我很难找到如何做到这一点,我当前的代码将无法工作。
<script src="inc/date-fi-FI.js" type="text/javascript"></script>
<script type="text/javascript">
function parseDate() {
var date = $('#date').val();
var parsedDate = Date.parse(date);
alert('Parsed date: '+parsedDate);
}
function jämförMedIdag (datum) {
if (datum == null || datum == "") {
alert('Inget datum!');
return;
}
/*resultat = Date.compare(Datum1,Datum2);
alert(resultat); */
var datum = Date.parse(datum);
var dagar = datum.getDate();
var månader = datum.getMonth();
var år = datum.getYear();
var nyttDatum = new Date();
nyttDatum.setFullYear(år,månader,dagar);
var idag = new Date();
if(nyttDatum>idag) {
var svar = nyttDatum - idag;
svar = svar.toString("dd.MM.yyyy");
alert(svar);
return(svar);
} else {
var svar = idag - nyttDatum;
svar = svar.toString("dd.MM.yyyy");
alert(svar);
return(svar);
}
}
</script>
此代码将尝试计算两个日期之间的差异,其中之一是今天。没有成功lz。
提前致谢!
我的最终代码(感谢 RobG!):
function dateDiff(a,b,format) {
var milliseconds = toDate(a) - toDate(b);
var days = milliseconds / 86400000;
var hours = milliseconds / 3600000;
var weeks = milliseconds / 604800000;
var months = milliseconds / 2628000000;
var years = milliseconds / 31557600000;
if (format == "h") {
return Math.round(hours);
}
if (format == "d") {
return Math.round(days);
}
if (format == "w") {
return Math.round(weeks);
}
if (format == "m") {
return Math.round(months);
}
if (format == "y") {
return Math.round(years);
}
}
它并不完全准确,但非常接近。我最终添加了一些插件来计算日、周、月、年或小时,任何人都可以自由复制和使用此代码。
I am trying to compare two dates which are in Finnish time form like this: dd.mm.YYYY or d.m.YYYY or dd.m.YYYY or d.mm.YYYY.
I am having a hard time finding out how to do this, my current code won't work.
<script src="inc/date-fi-FI.js" type="text/javascript"></script>
<script type="text/javascript">
function parseDate() {
var date = $('#date').val();
var parsedDate = Date.parse(date);
alert('Parsed date: '+parsedDate);
}
function jämförMedIdag (datum) {
if (datum == null || datum == "") {
alert('Inget datum!');
return;
}
/*resultat = Date.compare(Datum1,Datum2);
alert(resultat); */
var datum = Date.parse(datum);
var dagar = datum.getDate();
var månader = datum.getMonth();
var år = datum.getYear();
var nyttDatum = new Date();
nyttDatum.setFullYear(år,månader,dagar);
var idag = new Date();
if(nyttDatum>idag) {
var svar = nyttDatum - idag;
svar = svar.toString("dd.MM.yyyy");
alert(svar);
return(svar);
} else {
var svar = idag - nyttDatum;
svar = svar.toString("dd.MM.yyyy");
alert(svar);
return(svar);
}
}
</script>
This code will try to calculate the difference between two dates, one of them being today. No success lolz.
Thanks in advance!
My final code (thanks RobG!):
function dateDiff(a,b,format) {
var milliseconds = toDate(a) - toDate(b);
var days = milliseconds / 86400000;
var hours = milliseconds / 3600000;
var weeks = milliseconds / 604800000;
var months = milliseconds / 2628000000;
var years = milliseconds / 31557600000;
if (format == "h") {
return Math.round(hours);
}
if (format == "d") {
return Math.round(days);
}
if (format == "w") {
return Math.round(weeks);
}
if (format == "m") {
return Math.round(months);
}
if (format == "y") {
return Math.round(years);
}
}
It is not fully accurate, but very close. I ended up adding some addons to it to calculate in day week month year or hour, anyone can freely copy and use this code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用 Datejs 和可选的 time.js 模块,则可以通过创建
TimeSpan
对象,使用以下代码运行计算:示例
当然,上面的内容也可以是如果您确实想更加简洁,可以将其简化为一行。
示例
希望这有帮助。
If you are using Datejs, and the optional time.js module, you can run your calculations with the following code by creating a
TimeSpan
object:Example
Of course the above could be simplified down to one line if you really want to be extra terse.
Example
Hope this helps.
如果您的日期是常见形式 d/m/y 或其某种变体形式的字符串,您可以使用:
您可能想要验证输入,也可能不验证输入,具体取决于您对所提供数据的一致性的信心程度。
编辑以回答评论
要允许使用不同的分隔符(例如句点(.)或连字符(-)),要拆分的正则表达式可以是:
日期将分别拆分为日期、月份和年份数字。这些部分将传递给 Date 构造函数以创建该日期的本地日期对象。由于 javascript 月份是零索引(一月是 0,二月是 1 等等),月份数必须减一,因此
--s[1]
。/Edit
要比较两个日期对象(即获取以毫秒为单位的差异),只需将一个对象减去另一个对象即可。如果您想要以天为单位的结果,则除以一天和一轮中的毫秒数(以考虑夏令时引起的任何微小差异)。
因此,如果您想查看今天和某个日期之间有多少天,请使用:
PS。 “芬兰”数据格式是世界上绝大多数不使用 ISO 格式日期的国家所使用的一种格式。
If your dates are strings in the common form d/m/y or some variation thereof, you can use:
You may want to validate the input, or not, depending on how confident you are in the consistency of the supplied data.
Edit to answer comments
To permit different separators (e.g. period (.) or hyphen (-)), the regular expression to split on can be:
The date will be split into date, month and year numbers respectively. The parts are passed to the Date constructor to create a local date object for that date. Since javascript months are zero indexed (January is 0, February is 1 and so on) the month number must be reduced by one, hence
--s[1]
./Edit
To compare two date objects (i.e get the difference in milliseconds) simply subtract one from the other. If you want the result in days, then divide by the number of milliseconds in a day and round (to allow for any minor differences caused by daylight saving).
So if you want to see how many days are between today and a date, use:
PS. The "Finnish" data format is the one used by the vast majority of the world that don't use ISO format dates.
使用
Date
对象:Using the
Date
object: