JavaScript 日期对象英国日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3117262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:19:13  来源:igfitidea点击:

JavaScript date objects UK dates

javascriptdate

提问by Linda

I have the following code

我有以下代码

 datePicker.change(function(){
        dateSet = datePicker.val();
        dateMinimum = dateChange();
        dateSetD = new Date(dateSet);
        dateMinimumD = new Date(dateMinimum);
        if(dateSetD<dateMinimumD){
            datePicker.val(dateMinimum);
            alert('You can not amend down due dates');
        }       
    })

dateSet = "01/07/2010" dateMinimum = "23/7/2010"

dateSet = "01/07/2010" dateMinimum = "23/7/2010"

Both are UK format. When the date objects are compared dateSetD should be less than dateMinimumD but it is not. I think it is to do with the facts I am using UK dates dd/mm/yyyy. What would I need to change to get this working?

两者都是英国格式。比较日期对象时,dateSetD 应该小于 dateMinimumD,但事实并非如此。我认为这与我使用英国日期 dd/mm/yyyy 的事实有关。我需要改变什么才能让它工作?

回答by T.J. Crowder

The JavaScript Dateconstructor doesn't parse strings in that form (whether in UK or U.S. format). See the specfor details, but you can construct the dates part by part:

JavaScriptDate构造函数不解析该格式的字符串(无论是英国格式还是美国格式)。有关详细信息,请参阅规范,但您可以部分构建日期:

new Date(year, month, day);

MomentJSmight be useful for dealing with dates flexibly. (This answer previously linked to this lib, but it's not been maintained in a long time.)

MomentJS可能对灵活处理日期很有用。(这个答案以前链接到这个 lib,但很长时间没有维护。)

回答by woggles

This is how I ended up doing it:

这就是我最终这样做的方式:

 var lastRunDateString ='05/04/2012'; \5th april 2012
 var lastRunDate = new Date(lastRunDateString.split('/')[2], lastRunDateString.split('/')[1] - 1, lastRunDateString.split('/')[0]);

Note the month indexing is from 0-11.

请注意,月份索引是从 0 到 11。

回答by JRT

var dateString ='23/06/2015';
var splitDate = dateString.split('/');
var month = splitDate[1] - 1; //Javascript months are 0-11

var date = new Date(splitDate[2], month, splitDate[0]);

回答by Sjoerd

  • Split the date into day, month, year parts using dateSet.split('/')
  • Pass these parts in the right order to the Date constructor.
  • 使用将日期拆分为日、月、年部分 dateSet.split('/')
  • 按正确的顺序将这些部分传递给Date 构造函数。

回答by Thea

Yes, there is problem with the date format you are using. If you are not setting a date format the default date that is used is 'mm/dd/yy. So you should set your preferred date formate when you create it as following when you create the date picker:

是的,您使用的日期格式有问题。如果您没有设置日期格式,则使用的默认日期是'mm/dd/yy. 因此,在创建日期选择器时,您应该在创建时设置首选日期格式,如下所示:

$(".selector" ).datepicker({ dateFormat: 'dd/mm/yyyy' });

or you can set it later as:

或者您可以稍后将其设置为:

$.datepicker.formatDate('dd/mm/yyyy');

回答by piyer

When you try to create a date object:

当您尝试创建日期对象时:

new Date(year, month, day, hours, minutes, seconds, milliseconds)

Example:

例子:

dateSetD = new Date(dateSet.year, dateSet.month, dateSet.day);

Note: JavaScript Date object's month starts with 00, so you need to adjust your dateset accordingly.

注意:JavaScript Date 对象的月份从 00 开始,因此您需要相应地调整您的日期集。