简体   繁体   English

将毫秒值添加到今天的日期并显示日期,月份和年份(日期,月份和年份各自为单独的值)

[英]Add millisecond value to today's date and display Date, And Month and Year (Date, Month and Year are each separate values)

I am trying to create a testing script in Selenium and I need to enter a date. 我想在Selenium中创建一个测试脚本,我需要输入一个日期。 I have figured out how to get the dates using: 我已经弄清楚如何使用以下方式获取日期:

storeEval var d=new Date(); storeEval var d = new Date(); d.getDate() CurrentDay d.getDate()当前日
store Eval var m=new Date(); store Eval var m = new Date(); (m.getMonth()+1) CurrentMonth (m.getMonth()+ 1)CurrentMonth
storeEval var y=new Date(); storeEval var y = new Date(); y.getFullYear() CurrentYear y.getFullYear()CurrentYear

Now I want want to create variables for times in the past and future. 现在我想要在过去和未来创建变量。 I have been told I can do so using milliseconds, which is amazing but the closest I can come is this: 有人告诉我,我可以使用毫秒,这是惊人的,但我最接近的是这个:

storeEval new Date().getTime()+604800000 //604800000- being 7 days in the future storeEval new Date()。getTime()+ 604800000 // 604800000-将来7天

I get back: 1350932638018 which is 7 days forward according to this amazing calculator I found. 我回来了:1350932638018,根据我发现的这个惊人的计算器 ,这是7天前进。

So, how do I take the number I found and extract the date, month and year as I did for today's date. 那么,我如何获取我找到的数字并提取日期,月份和年份,就像我今天所做的那样。

If your future date is stored in the variable d then it should be as easy as: 如果您的未来日期存储在变量d那么它应该像以下一样简单:

var n = new Date(d);

or if it isn't stored in a variable, then maybe something like this? 或者如果它没有存储在变量中,那么可能是这样的?

var n = new Date(Date().getTime()+604800000);

And then now n is a date object and you should be able to use the .getFullYear() methods. 然后现在n是一个日期对象,你应该能够使用.getFullYear()方法。

Take a look at this fiddle and see if it helps: http://jsfiddle.net/wVVmw/ 看看这个小提琴,看看它是否有帮助: http//jsfiddle.net/wVVmw/

use toDateString() 使用toDateString()

So, 所以,

var newDate = (Date().getTime()+604800000).toDateString();

should return Mon Oct 22 2012 应该返回2012年10月22日星期一

I don't know selenium, but it looks like JavaScript. 我不知道selenium,但它看起来像JavaScript。

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

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