简体   繁体   English

jQuery日期比较以英国日期格式

[英]jquery date comparison for uk date format

Is there a relatively straightforward way to do a date comparison, using jquery , on a UK formatted date? 有没有一种相对简单的方法,可以使用jquery英国格式的日期上进行日期比较? I'm trying to check if the number of days selected via a date input field is greater than or equal to a set number. 我正在尝试检查通过日期输入字段选择的天数是否大于或等于设置的数字。 Unfortunately I can't change the date format and need to work with dd/mm/yy . 不幸的是,我无法更改日期格式,需要使用dd/mm/yy

UPDATE : the date I am working with is already in UK format dd/mm/yy - I am trying to convert this into a format which can be compared with the current date, to find number of days elapsed. 更新 :我正在使用的日期已经是英国格式dd/mm/yy我正在尝试将其转换为可以与当前日期进行比较的格式,以查找经过的天数。

Working solution: 工作解决方案:

function checkDaysElapsed (input) {

                var datePart = input.split("/");
                year = datePart[2]; // get only two digits
                month = datePart[1]; 
                day = datePart[0];

                var date1 = new Date(year,month-1,day); 

                var currentTime = new Date();
                var month2 = currentTime.getMonth();
                var day2 = currentTime.getDate();
                var year2 = currentTime.getFullYear();
                var date2 = new Date(year2,month2,day2);
                console.log(year + '/' + month + '/' + day);
                console.log(year2 + '/' + month2 + '/' + day2);

                delta = date2 - date1; 
                // delta is the difference in milliseconds 
                delta_in_days = Math.round(delta / 1000 / 60 / 60/ 24); 

                return delta_in_days;
        }

After you have the date in the correct format, you will also need to compare it to the other date. 日期以正确的格式显示后,您还需要将其与其他日期进行比较。 To do this, you might want to cast the date string to a Date() object. 为此,您可能需要将日期字符串转换为Date()对象。

Also note, that as soon as you have created a Date object, the actual format is no longer relevant. 还要注意,一旦创建了Date对象,实际的格式就不再相关了。

First you need to split the date into its components, using string.split('/') 首先,您需要使用string.split('/')将日期分割成多个部分

var date1 = new Date(year,month-1,day),
var date2 = new Date(year2,month2-1,day2);
delta = date2 - date1;
// delta is the difference in milliseconds
delta_in_days = Math.round(delta / 1000 / 60 / 60/ 24);

use this plugin to format the dates to pass to the server via ajax for accurate date comparission formatDate 使用此插件格式化日期以通过ajax传递到服务器,以获取准确的日期比较formatDate

and call like this 像这样打电话

var start = $.format.date($('#startDate').val(), "dd/MM/yyyy");
var finish= $.format.date($('#finish').val(), "dd/MM/yyyy");

$.ajax({
        cache: true,
        type: 'post',
        async: false,
        data: { start: start, finish: finish },
        url: '/controller/CheckDates',
        success: function (msg) {

            //msg will be the days

        }
    });

I've done it this way due to inaccuracies from experience, there may be a more efficient way 由于经验上的不正确,我已经这样做了,可能会有更有效的方法

this is how you set the date format to dd/mm/yy: 这是将日期格式设置为dd / mm / yy的方式:

$(function () {
    $(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM