Watin :获取日期时间并执行计算
我有一个文本字段,其日期格式为“12/23/2010”。我是否可以使用 watin 获取数字 23,即从文本字段获取数字;我将像这样使用它。
1.Get datetime 12/23/2010 and get number '23'
2.substract 2 from 23 and store it somewhere[ie: 23 - 2 = 21]
3.Insert the new datetime number [ie:12/21/2010 ]
string myDate = browser.TextField(Find.ByName("myTextField")).Value;
DateTime time = = new DateTime();
time2 = time - 2;
browser.TextField(Find.ByName("myTextField")).TypeText(time2);
这可能吗?或者我应该寻找另一种方式。请用户插入数据。
I have a textfield that has a date with the format "12/23/2010".Is there away for me to get the number 23 using watin ie get number from textfield;i'm gonna use it like this.
1.Get datetime 12/23/2010 and get number '23'
2.substract 2 from 23 and store it somewhere[ie: 23 - 2 = 21]
3.Insert the new datetime number [ie:12/21/2010 ]
string myDate = browser.TextField(Find.ByName("myTextField")).Value;
DateTime time = = new DateTime();
time2 = time - 2;
browser.TextField(Find.ByName("myTextField")).TypeText(time2);
Is this possible?or should i be looking to another way.Ask the user to insert the data instead.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试以字符串形式获取日期值
将其转换为日期时间并使用 AddDays 我们可以使用负值或正值
并将其插入文本框
就是这样,谢谢
Try getting the value of the date as string
Convert it to datetime and use AddDays we can use negative or positive value
And insert it into textbox
That's it thanks
您应该使用
DateTime.Parse
、DateTime.TryParse
、DateTime.ParseExact
或DateTime.TryParseExact
来解析文本到DateTime
。如果解析失败表明代码中某处失败(考虑到这是一个测试,这可能就是这里的情况),我怀疑
DateTime.ParseExact
是最合适的方法,提供预期的格式、区域性ETC。You should use
DateTime.Parse
,DateTime.TryParse
,DateTime.ParseExact
orDateTime.TryParseExact
to parse from text to aDateTime
.If a failure to parse indicates a failure in the code somewhere (which is probably the case here, given that it's a test) I suspect
DateTime.ParseExact
is the most appropriate approach, providing the expected format, culture etc.如果你想要的是从日期中减去 2 天,我会这样做:
当然,如果你确定你拥有的字符串是有效的日期。如果可能不是,那么您应该按照 Skeet 的建议进行操作,即首先使用 try 解析,检查返回值是否为 true,如果是,则执行其余操作,如果不是,则发送错误信息。
if what you want is to subtract 2 days from a date I would do it like this:
This is of course if you are sure the string you have IS a valid date. If it might not be, then you should do what the Skeet recommends, which is using first a try parse, checking if the return value is true, and if it is, then do the rest, and if it is not, send an error message.
考虑写作
consider writing