在javascript中将字符串与日期时间进行比较时出现问题. [英] problem comparing string with datetime in javascript.

查看:56
本文介绍了在javascript中将字符串与日期时间进行比较时出现问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function TimeSpentForFutureDate() {
                var toDate = new Date();
                toDate.setMinutes(0);
                toDate.setSeconds(0);
                toDate.setHours(0);
                toDate.setMilliseconds(0);
//Here after selecting future date also, this condition is failing.The textbox containing future date
                if (document.getElementById('<%= txtDate.ClientID%>').value > toDate) {
                    var timespent = jPrompt('Enter Time Spent:', '', 'Enter Time Spent', function (r) {
                        if (r) {
                            document.getElementById('<%= hiddenFieldFutureDateSelectTimeSpent.ClientID%>').value = r;
                            jAlert('You entered ' + r);
                        }
                        else {
                            var todaysDate = new Date();
                            jAlert('You had not entered the Time Spent', 'Message');
                        }
                    });
                }
                else {
                    document.getElementById('<%= hiddenFieldFutureDateSelectTimeSpent.ClientID%>').value = timespent;
                    document.getElementById('<%= txtDate.ClientID%>').value = toDate;
                }
            }



在上面的代码中,im检查文本框"txtDate"是否包含将来的日期,即比今天的日期大的日期,它将提示输入所花费的时间,然后将所花费的时间存储在隐藏字段中. > 我无法将字符串转换为日期时间以进行比较.请指导我为解决此问题将进行哪些更改.

在此先感谢您.



In the above code i m checking that if text box ''txtDate'' will contain a future date i.e date greater then today''s date it will prompt for entering time spent and then store that time spent into an hidden field.
I m not able to convert string into date time for comparision. Please guide me that what the changes i ll make to solve this issue.

Thanks in advance.

推荐答案

首先,您应该尝试以从未将字符串与DateTime比较的方式设计代码.您应该使用数据,而不是代表数据的字符串.仅当您将某些数据输出到屏幕并且永远不要将其用作输入时,字符串才应出现.但是,在某些情况下,您想将字符串解析为DateTime.

您可以使用方法System.DateTime.ParseSystem.DateTime.TryParseSystem.DateTime.ParseExactSystem.DateTime.TryParseExact之一来完成此操作.如果使用ParseExactTryParseExact,则输入格式应与输出格式完全匹配,但是当您知道确切的输入格式,该格式与标准格式不匹配并且需要避免歧义时,这很有用.

有关时间格式说明符,请参见:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx [ ^ ].

请参阅: http://msdn.microsoft.com/en-us/library/system.datetime .aspx [^ ].

另请参见有关标准,自定义和与文化相关的格式以及CultureInfo的作用:
http://msdn.microsoft.com/en-us/library/8tfzyc64.aspx [ ^ ].

请注意代码示例.

—SA
First of all, you should try to design your code the way you never compare string with DateTime. You should work with data, not strings representing data. The strings should appear only when you output some data to screen and never use it as input. Nevertheless, there are some cases where you want to parse a string as DateTime.

You can do it with one of the methods System.DateTime.Parse or System.DateTime.TryParse, System.DateTime.ParseExact or System.DateTime.TryParseExact. If you are using ParseExact or TryParseExact, your input format should match the output format exactly, but this is useful when you know the exact input format, the format does not match standard formats and you need to avoid the ambiguity.

For time format specifiers, please see:
http://msdn.microsoft.com/en-us/library/az4se3k1.aspx[^],
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx[^].

Please see: http://msdn.microsoft.com/en-us/library/system.datetime.aspx[^].

See also about standard, custom and culture-related formatting and the role of CultureInfo:
http://msdn.microsoft.com/en-us/library/8tfzyc64.aspx[^].

Pay attention for the code samples.

—SA


这篇关于在javascript中将字符串与日期时间进行比较时出现问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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