Home >Web Front-end >JS Tutorial >JavaScript date formatting example sharing_Basic knowledge

JavaScript date formatting example sharing_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:57:051068browse

复制代码 代码如下:

/**
* Format date
* @param {Object} ms represents the number of milliseconds between the specified date and midnight of January 1, 1970, universal standard time
* @return year-month-date hh:mm
*/ 
 Util.parseToDate = function(ms){   
        var date = (new Date(parseInt(ms))); 
        return formatDate(date,"yyyy-MM-dd mm:hh");  
 };  
/**
* Format date
* <br> * yyyy-------year <br> * MM---------month <br> * dd-- -------Day <br> * hh---------Hour <br> * mm---------Minute <br> * Such as: Util.formatDate(new Date () , 'yyyy-MM-dd mm:hh'); <br> * or Util.formateDate(new Date(), 'yyyy/MM/dd mm/hh'); <br> * * @param {Date}date Date object that needs to be formatted
* @param {Object} style style
* @return Returns the formatted current time
*/  
Util.formatDate = function(date, style){ 
      var y = date.getFullYear();  
      var M = "0" (date.getMonth() 1);  
      M = M.substring(m.length - 2); 
      var d = "0" date.getDate(); 
      d = d.substring(d.length - 2);  
      var h = "0" date.getHours();  
      h = h.substring(h.length - 2);  
      var m = "0" date.getMinutes();  
      m = m.substring(m.length - 2);  
      return style.replace('yyyy', y).replace('MM', M).replace('dd', d).replace('hh', h).replace('mm', m); 
 }
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn