关闭

javascript之日期

标签: Javascript
71人阅读 评论(0) 收藏 举报
分类:

1.日期格式化代码:

           Date.prototype.Format = function (fmt) { //author: meizz 
		    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;
		}

2.使用方法:
           var date=new Date(毫秒);
	   var datefor = date.Format("yyyy-MM-dd");

3.获取当天的开始与结束时间

	     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			  Calendar todayStart = Calendar.getInstance();  
		        todayStart.set(Calendar.HOUR_OF_DAY, 0);  
		        todayStart.set(Calendar.MINUTE, 0);  
		        todayStart.set(Calendar.SECOND, 0);  
		        todayStart.set(Calendar.MILLISECOND, 0);  
		        Date ds = new Date(todayStart.getTime().getTime());
		        Date date1=sdf.parse(sdf.format(ds));
			
			   Calendar todayEnd = Calendar.getInstance();  
		        todayEnd.set(Calendar.HOUR_OF_DAY, 23);  
		        todayEnd.set(Calendar.MINUTE, 59);  
		        todayEnd.set(Calendar.SECOND, 59);  
		        todayEnd.set(Calendar.MILLISECOND, 999);  
		        Date d = new Date(todayEnd.getTime().getTime());
		        Date date2=sdf.parse(sdf.format(d));
4.获取当前周的周一与周日

	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
			  Calendar monday = Calendar.getInstance();
			  int mondayofweek = monday.get(Calendar.DAY_OF_WEEK) - 1;
			  if (mondayofweek == 0)
			   mondayofweek = 7;
			  monday.add(Calendar.DATE, -mondayofweek + 1);
			  Calendar funday = Calendar.getInstance();
			  int fundayofweek = funday.get(Calendar.DAY_OF_WEEK) - 1;
			  if (fundayofweek == 0)
			   fundayofweek = 7;
			  funday.add(Calendar.DATE, -fundayofweek + 8);
			String f1=sdf.format(monday.getTime());
			Date date1 = sdf.parse(f1);
			String f2=sdf.format(funday.getTime());
			Date date2 = sdf.parse(f2);



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:32122次
    • 积分:1184
    • 等级:
    • 排名:千里之外
    • 原创:73篇
    • 转载:17篇
    • 译文:14篇
    • 评论:2条
    最新评论