时间格式化

转自: http://www.cnblogs.com/tugenhua0707/p/3776808.html

1. 将日期转换为指定的格式:比如转换成 年月日时分秒 这种格式:yyyy-MM-dd hh:mm:ss 或者 yyyy-MM-dd。当然是网上的方法,只是总结下。

  可以为Date原型添加如下的方法:

Date.prototype.format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt)) {
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
}
for(var k in o) {
if(new RegExp("("+ k +")").test(fmt)){
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
}
}
return fmt;
}

比如我们可以这样调用下:

var time1 = new Date().format("yyyy-MM-dd hh:mm:ss");
console.log(time1);

------------------------------

如果传过来的是时间戳,比如1498533751000,想要转成2017-06-27 11:22:22,就先要将时间戳封装成Date对象,比如:

var date = new Date(dateTimeStr);   // 把时间戳封装成Date对象,然后调用方法:

return date.format("yyyy-MM-dd hh:mm:ss");

运行如下:

也可以转换成 ”年月日”的格式 

var time2 = new Date().format("yyyy-MM-dd");
console.log(time2);

运行如下:

 

2. 将指定的日期转换为"年月日"的格式,代码如下:

    var oldTime = (new Date("2012/12/25 20:11:11")).getTime();
    var curTime = new Date(oldTime).format("yyyy-MM-dd");
    console.log(curTime);

   运行如下:

3. 将 "时间戳" 转换为 "年月日" 的格式.

  比如如下代码: 

    var da = 1402233166999;
    da = new Date(da);
    var year = da.getFullYear()+'年';
    var month = da.getMonth()+1+'月';
    var date = da.getDate()+'日';
    console.log([year,month,date].join('-'));

运行如下:

 

-----------------------------------------------------------

方法: 修改jquery插件的日期格式

----------------------------------------------------------

范例:Datelong的转换

package cn.mldn.demo ;

import java.util.Date;

public class TestDemo {

public static void main(String[] args) {

long cur = System.currentTimeMillis() ; //取得当前的日期时间以long型返回

Date date = new Date(cur) ;

System.out.println(date); //Wed May 18 15:19:16 CST 2016

System.out.println(date.getTime()); //1463556019396

}

}

 范例:将日期格式化显示(Date型数据变为String型数据)

 

package cn.mldn.demo ;

import java.text.SimpleDateFormat;

import java.util.Date;

public class TestDemo {

public static void main(String[] args) {

Date date = new Date() ;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;

String str = sdf.format(date) ; //Date型变为String

System.out.println(str); //2016-05-18 15:49:20.009

}

} 

 范例:将字符串转换为日期(String型数据变为Date型数据)

 

package cn.mldn.demo ;

import java.text.SimpleDateFormat;

import java.util.Date;

public class TestDemo {

public static void main(String[] args) throws Exception {

String str = "2001-11-11 11:11:11.111" ;

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS") ;

Date date = sdf.parse(str) ; //将字符串变为日期型数据

System.out.println(date); //Sun Nov 11 11:11:11 CST 2001

}

} 

范例:取得当前的日期时间

package cn.mldn.demo ;

import java.util.Calendar;

public class TestDemo {

public static void main(String[] args) throws Exception {

Calendar cal = Calendar.getInstance() ; //取得Calendar类对象

StringBuffer sbf = new StringBuffer() ;

//Calendar类中的方法:public int get(int field)

sbf.append(cal.get(Calendar.YEAR)).append("-") ;

sbf.append(cal.get(Calendar.MONTH) + 1).append("-") ;

sbf.append(cal.get(Calendar.DAY_OF_MONTH) + 5).append(" ") ;

sbf.append(cal.get(Calendar.HOUR_OF_DAY)).append(":") ;

sbf.append(cal.get(Calendar.MINUTE)).append(":") ;

sbf.append(cal.get(Calendar.SECOND)) ;

System.out.println(sbf); //2016-5-18 16:25:50

}

 

posted @ 2017-03-17 16:49  半生戎马,共话桑麻、  阅读(133)  评论(0)    收藏  举报
levels of contents