2015-12-14 3 views
2

У меня есть текущий скрипт. var date 2 необходимо автоматически захватить текущую дату, а затем выполнить расчет. Я пробовал много вещей, не повезло, пожалуйста, помогите.как вставить текущую дату в html-скрипт

<script> 
 

 
// Here are the two dates to compare 
 
var date1 = '2015-09-08'; 
 
var date2 = '2015-12-13'; 
 

 
// First we split the values to arrays date1[0] is the year, [1] the month and [2] the day 
 
date1 = date1.split('-'); 
 
date2 = date2.split('-'); 
 

 
// Now we convert the array to a Date object, which has several helpful methods 
 
date1 = new Date(date1[0], date1[1], date1[2]); 
 
date2 = new Date(date2[0], date2[1], date2[2]); 
 

 
// We use the getTime() method and get the unixtime (in milliseconds, but we want seconds, therefore we divide it through 1000) 
 
date1_unixtime = parseInt(date1.getTime()/1000); 
 
date2_unixtime = parseInt(date2.getTime()/1000); 
 

 
// This is the calculated difference in seconds 
 
var timeDifference = date2_unixtime - date1_unixtime; 
 

 
// in Hours 
 
var timeDifferenceInHours = timeDifference/60/60; 
 

 
// in weeks :) 
 
var timeDifferenceInWeeks = timeDifferenceInHours/24/7; 
 

 
document.write(Math.ceil(timeDifferenceInWeeks)); 
 
</script>

+0

Не определяйте 'уаг date2 = '2015-12-13', 'следовательно, разделение операция не будет there..Just' вар date2 = новый Date(); 'Попробуйте это : https://jsfiddle.net/09aoz9u1/ – Rayon

+0

Если я использую вашу идею, расчет не произойдет. – aumandg

+0

Вы проверили скрипку? – Rayon

ответ

0

Я перешел на PHP-код, и вот что я получил, чтобы работать.

<? 
 
$strtDate = '2015-09-08'; 
 
$endDate = date('Y-m-d'); 
 

 
//$endDate = 'new Date();'; 
 
$startDateWeekCnt = round(floor(date('d',strtotime($strtDate))/7)) ; 
 

 
// echo $startDateWeekCnt ."\n"; 
 
$endDateWeekCnt = round(ceil(date('d',strtotime($endDate))/7)) ; 
 

 
//echo $endDateWeekCnt. "\n"; 
 
$datediff = strtotime(date('Y-m',strtotime($endDate))."-01") - strtotime(date('Y-m',strtotime($strtDate))."-01"); 
 
$totalnoOfWeek = round(floor($datediff/(60*60*24))/7) + $endDateWeekCnt - $startDateWeekCnt ; 
 
echo $totalnoOfWeek ."\n"; 
 
?>

1

Вы можете использовать new Date(), чтобы получить текущую дату. обратитесь пожалуйста фрагмент.

<script> 
 

 
// Here are the two dates to compare 
 
var date1 = '2015-09-08'; 
 
var date2 = '2015-12-13'; 
 

 
// First we split the values to arrays date1[0] is the year, [1] the month and [2] the day 
 
date1 = date1.split('-'); 
 
date2 = date2.split('-'); 
 

 
// Now we convert the array to a Date object, which has several helpful methods 
 
date1 = new Date(date1[0], date1[1]-1, date1[2]); 
 
date2 = new Date(date2[0], date2[1]-1, date2[2]); 
 

 
// We use the getTime() method and get the unixtime (in milliseconds, but we want seconds, therefore we divide it through 1000) 
 
date1_unixtime = parseInt(date1.getTime()); 
 
date2_unixtime = parseInt(date2.getTime()); 
 
date3_unixtime = parseInt((new Date()).getTime()); 
 

 
// This is the calculated difference in seconds 
 
var timeDifference = date2_unixtime - date1_unixtime; 
 
    
 
var timeDifferenceInWeeks1 = timeDifference/(1000*60*60*24*7); 
 
var timeDifferenceInWeeks2 = (date3_unixtime - date2_unixtime)/(1000*60*60*24*7); 
 

 
document.write(Math.ceil(timeDifferenceInWeeks1)+'--'+Math.ceil(timeDifferenceInWeeks2)); 
 
</script>

+0

var date2 = new Date(); не работает, если вы запустите первый код выше, вы получите ответ 14. Если я поставлю var date2 = new Date(); вместо Var date2 = '2015-12-13'; ответ не разрешен. Когда я запускаю свой код, я тоже не получаю правильный ответ. ваш код дает 14-1, ответ должен быть 14 или 15 в зависимости от текущей даты. – aumandg

1

просто использовать

var date2 = new Date(); 

теперь date2 держит текущую дату и время с вашего компьютера.

Смежные вопросы