格式化日期YYYY-MM-DDTHH:MM:SSZ in js

时间:2014-11-15 17:44:17

标签: javascript jquery html

我需要在javascript中验证日期是以下一格式在html中引入的 YYYY-MM-DDTHH:MM:SSZ

这是我的Html

<div class="row">
      <div class="col-sm-8">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title">Edit Todo</h3>
          </div>
          <div class="panel-body">
          <div class="input-group input-group-sm">
              <span class="input-group-addon">ID</span> <input type="textbox" class="form-control" id="id_to_edit">
            </div> 
            <div class="input-group input-group-sm">
              <span class="input-group-addon">Date</span> <textarea type="text" class="form-control" placeholder="YYYY-MM-DDTHH:MM:SS" id="date_to_edit"></textarea>
            </div>

            <span id="update_result"></span>

          </div>
        </div>
      </div><!-- /.col-sm-8 -->
      <div class="col-sm-4">
        <button type="button" class="btn btn-info" id="button_edit_todo">Edit Todo</button>
        <div class="input-group input-group-sm">
          <span class="input-group-addon">ID</span>
          <input type="text" class="form-control" id="todo_id_edit">
        </div>
        <button type="button" class="btn btn-info" id="button_save_todo">Save Todo</button>
      </div>
    </div>

这是我的javascript

$("#button_edit_todo").click(function(e){
$("#update_result").text('');
e.preventDefault();
if($('#todo_id_edit').val() == ""){
    $('<div class="alert alert-info"> <strong>Oh!</strong> Debes proporcionar una ID </div>').appendTo($("#update_result"));
}else if (isNaN($('#todo_id_edit').val())){
    $('<div class="alert alert-info"> <strong>Oh!</strong> La ID debe ser un numero </div>').appendTo($("#update_result"));
}else{
    getTodoToEdit($('#todo_id_edit').val());
}

});

$("#button_save_todo").click(function(e){
$("#update_result").text('');
e.preventDefault();
var todoEditado;
if($('#id_to_edit').val() == "" || $('#summary_to_edit').val()=="" || $('#todo_id_edit').val()==""){
    $('<div class="alert alert-info">Debes rellenar los campos ID y Summary. El campo de busqueda debe contener la ID del actual. </div>').appendTo($("#update_result"));
}else if (isNaN($('#id_to_edit').val())){
    $('<div class="alert alert-info">La ID editada debe ser un numero </div>').appendTo($("#update_result"));
}else{
    todoEditado = {
        "id" : $("#id_to_edit").val(),

        "date" : $("#date_to_edit").val(),}
    editTodo(todoEditado);
}});

如果有人可以帮助验证这一天,我非常感激。

由于

3 个答案:

答案 0 :(得分:0)

JS中的

    function Datepicker() {
     this._curInst = null; // The current instance in use
      this._keyEvent = false; // If the last event was a key event
    this._disabledInputs = []; // List of date picker inputs that have been disabled
     this._datepickerShowing = false; // True if the popup picker is showing , false   
      if not
        this._inDialog = false; // True if showing within a "dialog", false if not
   this._mainDivId = "ui-datepicker-div"; // The ID of the main datepicker division
    this._inlineClass = "ui-datepicker-inline"; // The name of the inline marker class
   this._appendClass = "ui-datepicker-append"; // The name of the append marker class
   this._triggerClass = "ui-datepicker-trigger"; // The name of the trigger marker 
  class
   this._dialogClass = "ui-datepicker-dialog"; // The name of the dialog marker class
   this._disableClass = "ui-datepicker-disabled"; // The name of the disabled covering 
  marker class
   this._unselectableClass = "ui-datepicker-unselectable"; // The name of the 
   unselectable cell marker class
  this._currentClass = "ui-datepicker-current-day"; // The name of the current 
  day         marker class
  this._dayOverClass = "ui-datepicker-days-cell-over"; // The name of the day hover   
  marker class

 this.regional = []; // Available regional settings, indexed by language code
   this.regional[""] = { // Default regional settings
    closeText: "Done", // Display text for close link
    prevText: "Prev", // Display text for previous month link
    nextText: "Next", // Display text for next month link
    currentText: "Today", // Display text for current month link
    monthNames: ["January","February","March","April","May","June",
        "July","August","September","October","November","December"], // Names of   
  months for drop-down and formatting
    monthNamesShort: ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", 
  "Oct", "Nov", "Dec"], // For formatting
    dayNames: ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday",   
 "Saturday"], // For formatting
    dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], // For formatting
    dayNamesMin: ["Su","Mo","Tu","We","Th","Fr","Sa"], // Column headings for days 
  starting at Sunday
    weekHeader: "Wk", // Column header for week of the year
    dateFormat: "dd/mm/yy", // See format options on parseDate
    firstDay: 0, // The first day of the week, Sun = 0, Mon = 1, ...
    isRTL: false, // True if right-to-left language, false if left-to-right
    showMonthAfterYear: false, // True if the year select precedes month, false for 
   month then year
    yearSuffix: "" // Additional text to append to the year in the month headers
     };

答案 1 :(得分:0)

使用momentjs

moment().format('MMMM Do YYYY, h:mm:ss a'); // November 15th 2014, 11:34:04 pm
moment().format('dddd');                    // Saturday
moment().format("MMM Do YY");               // Nov 15th 14
moment().format('YYYY [escaped] YYYY');     // 2014 escaped 2014
moment().format();                          // 2014-11-15T23:34:04+03:30

答案 2 :(得分:0)

只需使用内置日期

function validate(str) {
  // Validate that str is in the format YYYY-MM-DDTHH:MM:SSZ.
  if(!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/.test(str)) {
    return false;
  }

  // Validate that the values are also correct date values
  var d = new Date(str);

  return !isNaN(d.getTime());
}