首页 > 代码库 > JS 中关于日期
JS 中关于日期
Fri Oct 31 18:00:00 UTC+0800 2008 转换 2008-10-31
function Todate(num) { //Fri Oct 31 18:00:00 UTC+0800 2008 num = num + ""; var date = ""; var month = new Array(); month["Jan"] = 1; month["Feb"] = 2; month["Mar"] = 3; month["Apr"] = 4; month["May"] = 5; month["Jan"] = 6; month["Jul"] = 7; month["Aug"] = 8; month["Sep"] = 9; month["Oct"] = 10; month["Nov"] = 11; month["Dec"] = 12; var week = new Array(); week["Mon"] = "一"; week["Tue"] = "二"; week["Wed"] = "三"; week["Thu"] = "四"; week["Fri"] = "五"; week["Sat"] = "六"; week["Sun"] = "日"; str = num.split(" "); date = str[5] + "-"; date = date + month[str[1]] + "-" + str[2]; return date; }
将时间戳处理成标准格式
function time_deal(time_stamp){ var year = time_stamp.substring(0,4); var month = time_stamp.substring(4,6); var day = time_stamp.substring(6,8); var new_time = year+"-"+month+"-"+day; return new_time; }
计算两个日期相差多少天
/** * 计算两个日期相差多少天 * @param strDateStart * @param strDateEnd * @returns */ function getDays(strDateStart,strDateEnd){ var strSeparator = "-"; //日期分隔符 var oDate1; var oDate2; var iDays; oDate1= strDateStart.split(strSeparator); oDate2= strDateEnd.split(strSeparator); var strDateS = new Date(oDate1[0], oDate1[1]-1, oDate1[2]); var strDateE = new Date(oDate2[0], oDate2[1]-1, oDate2[2]); iDays = parseInt(Math.abs(strDateS - strDateE ) / 1000 / 60 / 60 /24)//把相差的毫秒数转换为天数 return iDays ; }
获取当前时间
/** * 获取当前时间 */ function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate; // + " " + date.getHours() + seperator2 + date.getMinutes() // + seperator2 + date.getSeconds(); return currentdate; }
JS 中关于日期
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。