JavaScript 添加日期函数
我对核心 JavaScript 不太了解,只是了解一点 jQuery。但我知道 jQuery 并不是我所需要的:
我想使用 getdate 函数来找出服务器的星期几。然后添加一堆子句,例如:
- 如果是星期一,则向日期添加 6 并以 MM/DD/YYYY 形式返回日期。
- 如果是星期二,则在日期上加 5,并以 MM/DD/YYYY 形式返回日期。
- 如果是星期三,则在日期上添加 4,并以 MM/DD/YYYY 形式返回日期。
依此类推,直到周日,它会添加 0。
所以假设今天是周一,它将返回 1/8/2012 在实际日期中,今天是星期日,因此它实际上会返回 1/1/2012
然后我只想调用 document.write
函数将其返回的 MM/DD/YYYY 写入我的 HTML 文档中。
有人可以帮助我吗?如果你需要我的话我可以澄清...
I don't really know too much about core JavaScript, just a dot of jQuery. But I know jQuery is not necessary for what I need here:
I want to use the getdate function to find out the server's day of the week. Then add a bunch of clauses like:
- if its Monday add 6 to the date and return the date in MM/DD/YYYY form.
- if its Tuesday add 5 to the date and return the date in MM/DD/YYYY form.
- if its Wednesday add 4 to the date and return the date in MM/DD/YYYY form.
and so on until Sunday when it will add 0.
So lets say todays Monday, it will return 1/8/2012
And in real dates today's Sunday so it will really return 1/1/2012
Then I just want to call a document.write
function to write the MM/DD/YYYY it returns into my HTML document.
Can anybody help me? I can clarify if you need me to...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
getDay() 返回星期几,星期日 = 0,星期一 = 1,等等。
所以说今天是星期一 getDay() 将返回 1,这意味着 daysToAdd 将是 5。
一旦我们知道我们想要多少天添加我们可以创建一个新日期并添加这些日期。为此,我们获取以毫秒为单位的今天,然后添加以毫秒为单位的天数 (daysToAdd)。
我们通过乘以 24*60*60*1000(一天中的毫秒数)将天转换为毫秒。
我在月份中添加 1,因为 JavaScript 返回基于 0 的月份,但出于显示目的,我们希望对其进行格式化,例如,一月是 1 而不是零。
您可以像这样在代码中实现 JavaScript:
您的 HTML 会是这样的:
您可以在这里找到 jsFiddle: jsFiddle
如果您想调整工作日,以便将星期一而不是星期日视为一周的开始,您可以在获取 weekDay 后执行以下操作:
getDay() returns the day of the week, Sunday = 0, Monday = 1, etc, etc.
So say today was Monday getDay() would return 1, which means daysToAdd would be 5.
Once we know how many days we want to add we can create a new date and add those days. We do this by getting today in milliseconds and then adding the number of days (daysToAdd) in milliseconds.
We convert days to milliseconds by multiplying by 24*60*60*1000 which is the number of milliseconds in a day.
I add 1 to the month because JavaScript returns 0 based month, but for display purposes we want to format it so that January for example is 1 not zero.
You could implement in your code like so, JavaScript:
Your HTML would be something like this:
You can find the jsFiddle here: jsFiddle
If you want to adjust the weekday so that you consider Monday the start of the week instead of Sunday you can do the following after you get the weekDay:
我认为,只要在 JavaScipt 中添加日期,我的“DateExtensions”库就可以很好地做到这一点。您可以在这里获取:
http://depressedpress.com/javascript-extensions/dp_dateextensions/
引用后,您可以调用“add()”作为任何有效日期的方法,并向其传递许多日期部分(秒、分钟、天、小时等)中的任何一个。因此,假设“curDate”是一个有效的 JavaScript 日期对象,您可以添加 5 天,如下所示:
newDate = curDate.add(5, "days");
使用负值将减去:
newDate = curDate.add(-5, "days");
一旦获得所需的日期,您可以使用库的 dateFormat() 方法来显示它,如下所示:
curDate.dateFormat("MM/DD/YYYY");
链接中有完整的文档。
星期几的整数值
至于获取您想要的整数值,实际上看起来更容易(并且您不需要“if”,只是一些数学运算)。 date 的 getDay() 方法返回星期几,其中星期日为“0”,星期六为“6”。因此,从星期日开始的一周通常为:
0,1,2,3,4,5,6
首先,您想要反转该比例。通过从值中减去 7(集合的成员总数)即可轻松完成此操作。这给你这个比例:
-7,-6,-5,-4,-3,-2,-1
我们越来越接近了。您也希望第一个值为零。 (我认为)执行此操作的最简单方法是获取该值除以成员总数的模(余数)。所有这些基本上所做的就是将“-7”设为零,然后将其余部分留给我们:
0,-6,-5,-4,-3,-2,-1
几乎完成了。最后,您不需要负数,因此需要使用 Math.abs() 方法消除符号(获取绝对值),从而得到我们想要的结果:
0,6,5,4,3,2,1
对于所有的讨论,实际代码非常紧凑:
Math.abs((cnt-7)%7)
将其包装到原始示例中给出:
newDate = curDate.add(Math.abs((curDate.getDay()-7)%7), "天");
服务器与客户端
但是,请牢记 nnnnnn 的评论:在 JavaScript 中,getDate() 函数获取其运行所在计算机的当前日期/时间 - 对于客户端网页,而不是服务器。
如果您确实想让客户计时,那么您就已经准备好了。然而,如果您确实需要服务器时间,那就太烦人了,甚至不可能。如果您拥有服务器,那么实际上并不难设置一条规则,将当前服务器包含在每个已完成请求的 cookie 中(然后您可以使用我的 cookie 库(也在上面的站点)来访问信息!
)更混乱,但根据服务器的不同,您也许还可以创建一个老式的服务器端包含,向每个页面添加一些 JavaScript(最好作为标头中的标记替换),将日期硬编码为全局变量。
您还可以创建一个返回当前服务器时间的 Web 服务,但与所传送的数据相比,其客户端开销非常大。
如果服务器不是您的(并且您无法让所有者提供上述内容),那么唯一真正可能的选择是直接进行 http 调用并检查 HTTP“日期”标头。然而,与回报相比,这方面的开销是巨大的,但这确实是唯一的方法。任何像这样的系统都必须非常灵活,但是因为任何特定服务器可能不会返回日期标头或者可能无法正确返回它。
即使它确实有效,请理解您可能仍然无法获得“服务器”时间 - 或者至少不是您想要的服务器。在分层架构中,例如,应用程序服务器可能会渲染页面并将其交给 Web 服务器返回 - 您将获得 Web 服务器时间,而不是应用程序服务器。任意数量的设备也可能重写标头(例如,通常使用专用 SSL 设备来卸载所有加密工作 - 这些设备通常会重写标头本身)。
很抱歉说得太技术性了——不幸的是,JavaScript 绝对是一个很少有“简单问题”的领域。 ;^)
祝你好运!
As fas as adding dates in JavaScipt my "DateExtensions" library does this well enough, I think. You can get it here:
http://depressedpress.com/javascript-extensions/dp_dateextensions/
Once refenced you can call "add()" as a method for any valid date and pass it any of many date parts (second, minutes, days, hours, etc). So assuming "curDate" is a valid JavaScript date object you can add 5 days like this:
newDate = curDate.add(5, "days");
Using a negative value will subtract:
newDate = curDate.add(-5, "days");
Once you get the date you want you can the use the library's dateFormat() method to display it like so:
curDate.dateFormat("MM/DD/YYYY");
There's full documentation at the link.
Integer Values for Day of Week
As for getting the integer value you want, it's actually easier that it looks (and you don't need an "if" just some math). The getDay() method of date returns the day of week with Sunday as "0" and Saturday as "6". So the week, from Sunday, would normally be:
0,1,2,3,4,5,6
First, you want to reverse that scale. That's easily done via subtraction by taking 7 (to total number of members of the set) from the value. This gives you this scale:
-7,-6,-5,-4,-3,-2,-1
We're getting closer. You want the first value to be zero as well. The simplest way (I think) to do this is to get the modulus (remainder) of the value by the total number of members. All this basically does is make "-7" a zero and leave the rest alone giving us this:
0,-6,-5,-4,-3,-2,-1
Almost done. Finally you don't want negative numbers so you need to use the Math.abs() method to eliminate the sign (get the absolute value) leaving us with our desired result:
0,6,5,4,3,2,1
For all the talk the acutual code is pretty compact:
Math.abs((cnt-7)%7)
Wrapping this into the original example gives us:
newDate = curDate.add(Math.abs((curDate.getDay()-7)%7), "days");
Server Vs Client
However take nnnnnn's comment to heart: in JavaScript the getDate() function gets the current date/time of the machine that it's running on - in the case of a web page that's the client, not the server.
If you actually meant the client time them you're set and done. If you really need the server time however that's annoying-to-impossible. If you own the server then it's actually not to hard to set up a rule that includes the current server in a cookie withing each fufilled request (you could then use my cookie library, also at the site above, to access the information!)
It's messier but depending on the server you might also be able to create an old-school server-side include that adds a bit of JavaScript to each page (preferably as a marked replace in the header) that hard-codes the date as a global variable.
You might also create a web service that returns the current server time but the client-overhead for that is insane compared to the data being delivered.
If the server's NOT yours (and you can't get the owner to provide the above) then the only real potential option is to do a straight http call and examine the HTTP "Date" header. Again however the overhead on this is immense compared to the return but it's really the only way. Any system like this would have to be very flexible however as any particular server might not return the date header or might not return it correctly.
Even if it does work understand that you might still not be getting the "server" time - or at least not the server you want. In a tiered architecture, for example an application server might render then page and hand it to a web server to return - you'd be getting the web server time, not the app server. Any number of appliances might also rewrite the headers (for example it's common to use dedicated SSL appliances to offload all the encryption work - these often re-write the headers themselves).
Sorry to get overly technical - JavaScript is definately one area where there's unfortunately rarely a "simple question". ;^)
Good Luck!