使用jquery操作日期

时间:2013-07-16 07:00:03

标签: jquery time

我有一个输入字段ReportTimeFrom,其值为09.07.2013,如何在该日期添加一周?

我用

$("#ReportTimeFrom").val()

获取值

2 个答案:

答案 0 :(得分:1)

请尝试以下示例并检查jSfiddle - > http://jsfiddle.net/ApfJz/13/

function parseDate(input) {
  var parts = input.split('.');
  return new Date(parts[2], parts[1]-1, parts[0]); // months are 0-based
}

$('#change').click(function () {
   var date = parseDate($("#ReportTimeFrom").val());

    // add one week to date
    date.setDate(date.getDate() + 7); 

    $('#ReportTimeFrom').val(("0" + date.getDate()).slice(-2) + '.' + (("0" + (date.getMonth() + 1))).slice(-2) + '.' + date.getFullYear());

});  

答案 1 :(得分:0)

您可以使用此功能

var dmy = $("#ReportTimeFrom").val().split("/"); // "24/06/2011" should be pulled from $("#DatePicker").val() instead
console.log($("#ReportTimeFrom").val());
var joindate = new Date(
    parseInt(dmy[2], 10),
    parseInt(dmy[1], 10) - 1,
    parseInt(dmy[0], 10)
);
console.log(joindate); // Fri Jun 24 2011 00:00:00 GMT+0500 (West Asia Standard Time) 
joindate.setDate(joindate.getDate() + 7); // substitute 1 with actual number of days to add
//$("#ReportTimeFrom").val(joindate)
console.log(joindate);
console.log(
    ("0" + joindate.getDate()).slice(-2) + "/" +
    ("0" + (joindate.getMonth() + 1)).slice(-2) + "/" +
    joindate.getFullYear());

工作演示Fiddle