在日期中添加天数

时间:2016-08-10 00:56:09

标签: jquery

我有一个在日期中添加天数的功能。但问题是.setDate不是一个函数。这是我的代码。

$('#term').on('change',function(){
    var datepo = Aug-10-2016
    var days = 35
    var po_date = $.datepicker.formatDate('yy-m-d', new Date(datepo));
    po_date.setDate(po_date + parseInt(days)) 

    alert(po_date)
})

3 个答案:

答案 0 :(得分:1)

formatDate需要Date对象http://php.net/manual/en/function.array-unshift.php

var strDate = "Aug-10-2016"; strDate = strDate.replace(/-/g, " "); var datepo = new Date(strDate); var days = 35; datepo.setDate(datepo.getDate() + days); var strFormatedPODate = $.datepicker.formatDate('yy-m-d', new Date(datepo)); 返回字符串https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate

尝试

package csv

import play.api.libs.json.{JsObject, Json}

import scala.io.Source
import scala.util.Try

object CsvParser extends App {

  //Because a bad row can either != 4 columns or the age can not be a int we want to return an option that will be ignored by our caller
  def toTuple(array : Array[String]): Option[(String, String, String, Int)] = {
    array match {
      //if our array has exactly 4 columns  
      case Array(name, media, gender, age) => Try((name, media, gender, age.toInt)).toOption
      // any other array size will be ignored
      case _ => None
    }
  }

  def toJson(line: String): Option[JsObject] = {
    val cols = line.split(",").map(_.trim)
    toTuple(cols) match {
      case Some((name: String, media: String, gender: String, age: Int)) => Some(Json.obj("Name" -> name, "Media" -> media, "Gender" -> gender, "Age" -> age))
      case _ => None
    }
  }

  def parseFile(file: String): List[JsObject] = {

    val bufferedSource = Source.fromFile(file)
    try { bufferedSource.getLines().map(toJson).toList.flatten } finally { bufferedSource.close() }

  }

  parseFile("my/csv/file/path")

}

请在此处查看CodePen:https://api.jqueryui.com/datepicker/#utility-formatDate

答案 1 :(得分:0)

试试这个

$('#term').on('change',function(){
    var datepo = 'Aug-10-2016';
    var date_obj = new Date;
    var days = 35;
    var po_date = $.datepicker.formatDate('yy-m-d', new Date(datepo));

    alert(po_date + ' ' + date_obj.setDate(parseInt(days)));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.2/jquery-ui.min.js"></script>

<input id="term" type="text" />

答案 2 :(得分:0)

我得到了最终答案:

$('#term').on('change',function(){
    var datepo = $('#podate').val();
    var days = $('#term option:selected').data('termdays');
    var po_date = new Date($.datepicker.formatDate('M-dd-yy', new Date(datepo)));
    var po = new Date(po_date.setDate(po_date.getDate() + parseInt(days)))
    var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var duedate = months[po.getMonth()]+'-'+po.getDate() +'-'+po.getFullYear();
   alert(duedate);
})