JavaScript date format

2010-03-17  来源:本站原创  分类:Web  人气:236 

DateFormat = (function(){
 var SIGN_REGEXP = /([yMdhsm])(\1*)/g;
 var DEFAULT_PATTERN = 'yyyy-MM-dd';
 function padding(s,len){
  var len =len - (s+'').length;
  for(var i=0;i<len;i++){s = '0'+ s;}
  return s;
 };
 return({
  format: function(date,pattern){
   pattern = pattern||DEFAULT_PATTERN;
   return pattern.replace(SIGN_REGEXP,function($0){
    switch($0.charAt(0)){
     case 'y' : return padding(date.getFullYear(),$0.length);
     case 'M' : return padding(date.getMonth()+1,$0.length);
     case 'd' : return padding(date.getDate(),$0.length);
     case 'w' : return date.getDay()+1;
     case 'h' : return padding(date.getHours(),$0.length);
     case 'm' : return padding(date.getMinutes(),$0.length);
     case 's' : return padding(date.getSeconds(),$0.length);
    }
   });
  },
  parse: function(dateString,pattern){
   var matchs1=pattern.match(SIGN_REGEXP);
   var matchs2=dateString.match(/(\d)+/g);
   if(matchs1.length==matchs2.length){
    var _date = new Date(1970,0,1);
    for(var i=0;i<matchs1.length;i++){
     var _int = parseInt(matchs2[i]);
     var sign = matchs1[i];
    switch(sign.charAt(0)){
     case 'y' : _date.setFullYear(_int);break;
     case 'M' : _date.setMonth(_int-1);break;
     case 'd' : _date.setDate(_int);break;
     case 'h' : _date.setHours(_int);break;
     case 'm' : _date.setMinutes(_int);break;
     case 's' : _date.setSeconds(_int);break;
    }
    }
    return _date;
   }
   return null;
  }
 });
})();

/ / Date formatted as a date / time string.
alert (DateFormat.format (new Date (), 'yyyy dd month in MM Day'));
/ / Start from the given string of text to produce a date.
alert (DateFormat.parse ('2010-03-17 ',' yyyy-MM-dd '));

Notes: parse method of handling a little bit bad, still a little problem. Similar yyyyMMdd not divided like the format can not resolve.

相关文章
  • JavaScript date format 2010-03-17

    DateFormat = (function(){ var SIGN_REGEXP = /([yMdhsm])(\1*)/g; var DEFAULT_PATTERN = 'yyyy-MM-dd'; function padding(s,len){ var len =len - (s+'').length; for(var i=0;i<len;i++){s = '0'+ s;} return s; }; return({ format: function(date,pattern){ patte

  • javascript to format date 2009-09-26

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTEN

  • JavaScript example: onkeyup, standard date format date seeking non-regular intervals 2010-03-05

    Here on the keyCode value to see A date format <HTML> <HEAD> <SCRIPT> /** * onkeyup Date time format set * onkeyup Event : Keyboard pressed, release the keyboard response events */ function setDateFormat(obj){ // Read here about the keyC

  • javascript date formcat increasingly format Extension 2010-07-26

    // Extend the Date of format Method Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, "d+" : this.getDate(), "h+" : this.getHours(), "m+" : this.getMinutes(), "s+" : this.getSec

  • javascript date formcat extended date format 2010-07-26

    // Extend the Date of format Method Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, "d+" : this.getDate(), "h+" : this.getHours(), "m+" : this.getMinutes(), "s+" : this.getSec

  • js javascript date select immediately put the number of days difference between the two dates shown, js calculating both the number of days between the date of 2009-04-20

    A calculation of the date of the bad js <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <META NAME="Generator" CONTENT="EditPlus">

  • javascript: time format 2010-05-06

    javascript: time format / ** * Extension of the Date, will specify the format of Date into String * Monthly (M), Day (d), 12 Hours (h), 24 Hours (H), minutes (m), seconds (s), Week (E), quarter (q) can be 1-2 placeholder symbol * Year (y) can be 1-4

  • jquery determine the date format 2010-07-01

    Help Need to use the jQuery library (currently version 1.3) and jQuery Datepicker library file (version 3.5.1 currently) To install jquery.datepick-zh-CN.js support the Chinese language pack Material prepared Date picker custom CSS style to control j

  • javascript Date object Date 2010-12-24

    Create a Date object: var objDate = new Date ([arguments list]); Form of the following 5 parameters: new Date ("month dd, yyyy hh: mm: ss"); new Date ("month dd, yyyy"); new Date (yyyy, mth, dd, hh, mm, ss); new Date (yyyy, mth, dd); n

  • bboss mvc framework, the controller using the method specified comment date format date example of the type parameter 2011-05-04

    bboss mvc framework, the controller using the method specified comment date format date example of the type parameters directly into the subject: 1. Controller method definition-DateConvertController /* * Copyright 2008 biaoping.yin * * Licensed unde

  • in `require ': no such file to load - date / format 2009-03-03

    Learn today ruby, the ruby download and install, and then began to refer to the relevant entry-tutorial section of a small process running directly on the command line: ruby-e "print 'hello, yiditushe'", after the carriage return, prompted the f

  • hibernate search control date format 2009-10-18

    With Notes @ DateBridge (resolution = Resolution.DAY) control the date format Very simple to view a source file hibernate search Cases @Field(store = Store.NO, index = Index.UN_TOKENIZED) @Column(name = "update_date") @DateBridge(resolution = Re

  • java date format handling Daquan Date format SimpleDateFormat 2010-03-29

    Java In the date format conversion /** * Convert java string .util.Date<br> * Support the format yyyy .MM.dd G 'at' hh:mm:ss z As '2002-1-1 AD at 22:10:59 PSD'<br> * yy/MM/dd HH:mm:ss As '2002/1/1 17:55:00'<br> * yy/MM/dd HH:mm:ss pm As

  • java in a simple produce for examination to determine the date format is correct 2009-11-20

    / ** * Check Date Format * @ Param date: you want to produce for examination of the object * @ Param dateFormat: you want to produce for examination of the date format "yyyy / MM" * @ Return * / public boolean isDateStringValid (String date, Str

  • Flex in the date format 2010-03-31

    Flex in the date format var dateFormatter:DateFormatter = new DateFormatter(); dateFormatter.formatString= "YYYY-MM-DD"; var str:String = dateFormatter.format(item.entryModifiedDate);

  • Common date format sql server 2010-02-24

    SQL Server Chinese version of the default date field is datetime format yyyy-mm-dd Thh: mm: ss.mmm For example: select getdate () 2004-09-12 11:06:08.177 Finishing the look inside SQL Server may often use the date format conversion method: For exampl

  • calculated according to age, function, javascript date 2010-04-23

    javascript date calculated according to age, function code is as follows: // Given birth date, calculate age function ages(str) { var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/); if(r==null)return false; var d= new Date(r[1],r[3]-1,r[4]);

  • When using the Jackson JSON conversion, the date format settings 2010-05-04

    Recently, Shi Yong Jackson JSON object conversion when the display time, date is not always displayed correctly, the output of date is a string of digits to represent the timestamp, not Fugeyaoqiu, Suoyixiangdao Jackson Yingdang You way to set the ou

  • mysql date format TIME_FORMAT (time, format) 2010-05-07

    Example: When your database date format is mm / dd / yyyy format or the other, when the date is difficult to compare the size of because mysql generally recognized yyyy-mm-dd or yyyy-mm-dd HH: ii: ss format comparison . So then you can use time_forma

  • oracle date format conversion 2010-05-07

    Sqlplus or sqlplusw in default under or programming process, execute the command, if a session is sure the same will produce the effect you want. If you want a whole database level get rid of need to modify the parameters of the default format file,