English 中文(简体)
如何建立能够回过来的功能?
原标题:How to create a function which can return Date Difference in javascript?

i 在两个日期之间产生以下功能:要求核实其正确性,以及如何找到一个月(多到30或31天)和一年。

function days_between(date1, date2,datepart) {

// The number of milliseconds in one day
 var ONE_DAY=0;
 if ( datepart === undefined ) {
  datepart =  D ;
iii

 if(datepart= Y )
 {     
 ONE_DAY = 1000 * 60 * 60 * 24 * 30 *12
 iii
 else if (datepart= M )
 {
 ONE_DAY = 1000 * 60 * 60 * 24 * 30
 iii
 else
 {
 ONE_DAY = 1000 * 60 * 60 * 24   //for day
 iii

// Convert both dates to milliseconds
var date1_ms = date1.getTime()
var date2_ms = date2.getTime()

// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)

// Convert back to days and return
return Math.round(difference_ms/ONE_DAY)

iii

最佳回答

如果您的申请需要大量时间操纵方法,你不妨考虑使用Datejs图书馆等内容。

如果列入日期图书馆的<代码>时间.js,你可以做如下工作:

var future = new Date().add({months: 5}); 
var now = new Date();

var span = new TimeSpan(future - now);

console.log("Days: ", span.getDays());

The time.jsscript is optional and is not included in the prepared /build/ edition. 您可直接从SVN Library下载。

问题回答

你们在作出这种改变之前的几天内应当转变。 和:

var d1 = new Date().getTime(),
d2 = new Date(2010, 11, 31).getTime();
alert(Math.round((d2-d1)/24/60/60/1000) +   days left for 2010 );




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签