将两个日期与javascript或datejs进行比较(日期差异) [英] Comparing two dates with javascript or datejs (date difference)

查看:323
本文介绍了将两个日期与javascript或datejs进行比较(日期差异)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试比较以下芬兰时间表格中的两个日期:dd.mm.YYYY或dmYYYY或dd.m.YYYY或d.mm.YYYY。



我很难找出如何做到这一点,我目前的代码将无法正常工作。

  < script src =inc / date-fi-FI.jstype =text / javascript>< / script> 
< script type =text / javascript>
函数parseDate(){
var date = $('#date')。val();
var parsedDate = Date.parse(date);
alert('解析日期:'+ parsedDate);
}
函数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();
varmå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>

此代码将尝试计算两个日期之间的差异,其中一个是今天。不要成功lolz。



提前感谢!



我的最终代码(感谢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);
}
}

这不完全准确,但非常接近。我最后添加了一些插件来计算在白天的一个星期或一个小时,任何人都可以自由地复制和使用这个代码。

解决方案

p>如果您的日期是d / m / y或其某些变体的通用格式的字符串,则可以使用:

 函数toDate(s){
var s = s.split('/');
返回新日期(s [2], - s [1],s [0]);
}

您可能想要验证输入,还是根据您的信心如何

编辑以回答评论



允许不同的分隔符(例如句点(。)或连字符( - )),要分割的正则表达式可以是:

  var s = s.splpl /[/\.-]/); 

日期将分别分为日期,月份和年份。这些部分被传递给Date构造函数,以创建该日期的本地日期对象。由于javascript月份为零索引(1月为0,2月为1,所以等等),月份号必须减少一个,因此 - s [1] 。 p>

/编辑



要比较两个日期对象(即获取毫秒的差异)只需从另一个减去一个。如果你希望结果在几天内,那么除以一天中的毫秒数(允许由夏令时引起的任何微小差异)。



所以如果你想看看今天和日期之间有多少天,请使用:

  function diffToToday(s){
var today = new Date();
today.setHours(0,0,0);
return Math.round((toDate(s) - today)/ 8.64e7);
}

alert(diffToToday('2/8/2011')); // -1
alert(diffToToday('2/8/2012')); // 365

PS。 芬兰数据格式是世界绝大多数使用ISO格式日期的格式。


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.

解决方案

If your dates are strings in the common form d/m/y or some variation thereof, you can use:

function toDate(s) {
  var s = s.split('/');
  return new Date(s[2], --s[1], s[0]);
}

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:

  var s = s.split(/[/\.-]/);

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:

function diffToToday(s) {
  var today = new Date();
  today.setHours(0,0,0);
  return Math.round((toDate(s) - today) / 8.64e7);
}

alert(diffToToday('2/8/2011')); // -1
alert(diffToToday('2/8/2012')); // 365

PS. The "Finnish" data format is the one used by the vast majority of the world that don't use ISO format dates.

这篇关于将两个日期与javascript或datejs进行比较(日期差异)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆