javascript选择并转换正确的日期格式

时间:2012-02-21 06:16:41

标签: javascript mysql date stored-procedures

我写了一个存储过程,&要执行此过程,我需要传递一个IN参数,其数据类型为'DATE'。

现在我将简要介绍一下我的IN参数。

假设我想查看“2011年12月”的出席情况,那么我需要将“IN”参数作为“Previous_month_of_last_date”传递,即“2011-11-30”。然后我的程序执行并给我整整12月的出勤细节。

希望你还在我身边。

现在我的问题出现了。

我在GUI下面提供了用户。User_Input

因此用户将在此选择'年'& '月',假设他希望看到'2011年12月'报告,那么他将选择'2011'作为年份,选择'12月'作为输入。

现在我想写一个JavaScript代码,它将把这些'年'和'月'作为输入,并将以“Previous_month's_last_date”格式转换今年和月份,例如'2011-11-30'for'2011 12月报告'。

那么我应该如何编写脚本来将正确的输入传递给我的程序。

任何建议都会有所帮助。

这是我的存储过程。

DELIMITER $$

USE `Sample`$$

DROP PROCEDURE IF EXISTS `Test`$$

CREATE DEFINER=`Sample`@`%` PROCEDURE `Test`(IN month_last_date DATE)
BEGIN
SELECT 
users.id AS Id,
users.first_name AS Employee,cl_territories.name AS Territory,
CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='01' THEN main.task
              WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 1 DAY),'%W')='SUNDAY' THEN 0
              WHEN main.cdate IS NULL THEN -1
         ELSE -2 END)
     WHEN -1 THEN 'NA'
     WHEN  0 THEN 'S'
     WHEN  1 THEN 'Y'
     WHEN  2 THEN 'C'
     WHEN  3 THEN 'S1'
     WHEN  4 THEN 'P'
     WHEN  5 THEN 'M'
     WHEN  6 THEN 'H'
     WHEN  7 THEN 'L'
END Day1,
CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='02' THEN main.task
              WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 2 DAY),'%W') ='SUNDAY' THEN 0
              WHEN main.cdate IS NULL THEN -1
         ELSE -2 END)
     WHEN -1 THEN 'NA'
     WHEN  0 THEN 'S'
      WHEN  1 THEN 'Y'
      WHEN  2 THEN 'C'
      WHEN  3 THEN 'S1'
      WHEN  4 THEN 'P'
      WHEN  5 THEN 'M'
      WHEN  6 THEN 'H'
      WHEN  7 THEN 'L'
END Day2,
.
.
. # upt o 31 days
CASE MAX(CASE WHEN DATE_FORMAT(main.cdate,'%d')='31' THEN main.task
              WHEN DATE_FORMAT(DATE_ADD(month_last_date, INTERVAL 31 DAY),'%W')='SUNDAY' THEN 0
              WHEN main.cdate IS NULL THEN -1
         ELSE -2 END)
     WHEN -1 THEN 'NA'
     WHEN  0 THEN 'S'
     WHEN  1 THEN 'Y'
     WHEN  2 THEN 'C'
     WHEN  3 THEN 'S1'
     WHEN  4 THEN 'P'
     WHEN  5 THEN 'M'
     WHEN  6 THEN 'H'
     WHEN  7 THEN 'L'
END Day31
FROM
users  LEFT JOIN users_cstm
ON users.id=users_cstm.id_c
LEFT JOIN cl_territories
ON users_cstm.cl_territories_id1_c=cl_territories.id
LEFT JOIN
(
SELECT DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')AS cdate,
  cl_doctor_call.created_by  AS emp,
  1     task     
  FROM 
  cl_doctor_call  
  WHERE 
  DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')>month_last_date AND
  DATE_FORMAT(cl_doctor_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)

  UNION ALL
  SELECT 
  DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d') AS  cdate,
  cl_chemist_call.created_by AS emp,
  2    task     
  FROM 
  cl_chemist_call
  WHERE 
  DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d')>month_last_date AND
  DATE_FORMAT(cl_chemist_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)

  UNION ALL
  SELECT 
  DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d') AS cdate,
  cl_stockist_call.created_by AS emp,
  3         task
  FROM 
  cl_stockist_call LEFT JOIN cl_stockist_call_cstm
  ON cl_stockist_call.id=cl_stockist_call_cstm.id_c
  WHERE 
  DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d')> month_last_date AND
  DATE_FORMAT(cl_stockist_call_cstm.calldate_c,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)

  UNION ALL
  SELECT DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d') AS cdate,
  cl_product_pramotional_call.created_by AS emp,
  4       task
  FROM cl_product_pramotional_call
  WHERE  
  DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d')>month_last_date AND
  DATE_FORMAT(cl_product_pramotional_call.date_entered,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY)

  UNION ALL

  SELECT DATE_FORMAT(meetings.date_start,'%Y-%m-%d') AS cdate,
  meetings.created_by AS emp,
  5        task
  FROM meetings
  WHERE  
  DATE_FORMAT(meetings.date_start,'%Y-%m-%d')> month_last_date AND
  DATE_FORMAT(meetings.date_start,'%Y-%m-%d') <= DATE_ADD(month_last_date, INTERVAL 30 DAY)

  UNION ALL
SELECT DATE_FORMAT(cl_holidays_structures.holiday_date,'%Y-%m-%d') AS cdate,
users.id AS emp,
6    task
FROM users LEFT JOIN users_cstm
ON users.id=users_cstm.id_c
LEFT JOIN cl_states
ON users_cstm.cl_states_id1_c=cl_states.id
LEFT JOIN cl_holidays_structures
ON cl_holidays_structures.cl_states_id_c=users_cstm.cl_states_id1_c
WHERE  
DATE_FORMAT(cl_holidays_structures.holiday_date,'%Y-%m-%d')> month_last_date AND
DATE_FORMAT(cl_holidays_structures.holiday_date,'%Y-%m-%d') <= DATE_ADD(month_last_date, INTERVAL 30 DAY)
UNION ALL
SELECT cl_leave_management_cstm.from_date_c+ INTERVAL td.days DAY cdate, 
users.id AS emp,
7    task
FROM
  cl_leave_management LEFT JOIN cl_leave_management_cstm
  ON cl_leave_management.id=cl_leave_management_cstm.id_c
LEFT JOIN users 
  ON cl_leave_management.created_by = users.id
JOIN temp_days td
  ON DATEDIFF(cl_leave_management_cstm.to_date_c, cl_leave_management_cstm.from_date_c) >= td.days
WHERE DATE_FORMAT(cl_leave_management_cstm.from_date_c,'%Y-%m-%d')> month_last_date AND
 DATE_FORMAT(cl_leave_management_cstm.from_date_c,'%Y-%m-%d')<=DATE_ADD(month_last_date, INTERVAL 30 DAY) AND
 cl_leave_management_cstm.status_c='Approved'

 )AS main 
 ON main.emp = users.id 
 WHERE DATE_FORMAT(users.date_entered,'%Y-%m-%d')<= month_last_date
 GROUP BY users.id ;
 END$$

DELIMITER ;

这是我的SP的样本输出

Employee  Year       Month        1  2   3   4   5   6    7.... 
 John     2011      December      Y  Y   Y   H   Y   L    S....
 Jenny    2011      December      Y  Y   Y   Y   Y   M    S....
  .
  .
  .
all employees with all, these columns

1 个答案:

答案 0 :(得分:0)

我创造了一个小提琴Here

<强> HTML

<form method="post" action="#" onsubmit="return false">
    <select id="year">
        <option value="2008">2008</option>
        <option value="2009">2009</option>
       <option value="2010">2010</option>
       <option value="2011">2011</option>
       <option value="2012">2012</option>
    </select>

    <select id="month">
        <option value="1">Jan</option>
        <option value="2">Feb</option>
        <option value="3">Mar</option>
        <option value="4">Apr</option>
        <option value="5">May</option>
        <option value="6">Jun</option>
        <option value="7">Jul</option>
        <option value="8">Aug</option>
        <option value="9">Sep</option>
        <option value="10">Oct</option>
        <option value="11">Nov</option>
        <option value="12">Dec</option>
</select>

<button id="filter" onclick="doFilter()">Filter</button>

<强> JAVASCRIPT

function doFilter()
{
    var theMonth = document.getElementById('month').value;
    var theYear = document.getElementById('year').value;  
    var d = new Date(theYear, (theMonth - 1), 0);
    var theDate=d.getFullYear()+" "+(d.getMonth()+1)+" "+d.getDate();
    alert(theDate);
}