js获取当前时间并转变格式
2017-07-11 16:39
211 查看
/** * @description 把当前时间转成 (年.月.日 时:分:秒)日期格式的 * @params 无 * @return 返回当前时间的日期格式,例如:2017.07.11 15:14:44 */ function getCurrentTime(){ var date = new Date(); 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 hours = date.getHours(); if (hours >=0 && hours <= 9) { if (hours == 0) { hours = "00"; } else{ hours = "0" + hours; } } var minutes = date.getMinutes(); if (minutes >=0 && minutes <= 9) { if (minutes == 0) { minutes = "00"; } else{ minutes = "0" + minutes; } } var seconds = date.getSeconds(); if (seconds >=0 && seconds <= 9) { if (seconds == 0) { seconds = "00"; } else{ seconds = "0" + seconds; } } var currentdate = date.getFullYear()+"."+ month+"."+strDate+" "+hours+":"+minutes+":"+ ":"+seconds; console.log(currentdate); //2017.07.11 15:14:44 return currentdate ; }
/** * @description 把传入的(年 月 日 时 分 秒 2017 7 5 13 8 5)转成无格式的日期(20170705130805) * @params year,month,strDate,hours,minutes,seconds 年 月 日 时 分 秒 例如:2017 7 5 13 8 5 * @return 返回传入参数的无格式日期 例如:20170705130805 */ function getCurrentTime(year,month,strDate,hours,minutes,seconds) { if(month >= 1 && month <= 9) { month = "0" + month; } if(strDate > 0 && strDate <= 9) { strDate = "0" + strDate; } if(hours >= 0 && hours <= 9) { if (hours == 0) { hours = "00"; } else{ hours = "0" + hours; } } if(minutes >= 0 && minutes <= 9) { if (minutes == 0) { minutes = "00"; } else{ minutes = "0" + minutes; } } if(seconds >= 0 && seconds <= 9) { if (seconds == 0) { seconds = "00"; } else{ seconds = "0" + seconds; } } var currentdate = year + month + strDate + hours + minutes + seconds; console.log(currentdate); return currentdate; }
/** * @description 方法入口 要求超时时间格式为20170705130805 * @params tiemOut 单位秒 例如:60 (60秒后超时) * @return */ function sendRequest(){ var curTime = new Date(); var curTimeB = getCurrentTime(curTime.getFullYear(),curTime.getMonth()+1,curTime.getDate(),curTime.getHours(),curTime.getMinutes(),curTime.getSeconds()); console.log("当前时间="+curTimeB);//打印这里为了对比转变后的超时时间 var oldTime = curTime .getTime(); //获取当前时间的毫秒值 //假如在1分钟后超时,转成毫秒值 var timeOut = oldTime + 60 * 1000;//假如超时时间为当前时间之后的60秒 console.log(timeOut); //获取超时时间的时间戳 var timeOutDate = new Date(timeOut); console.log(timeOutDate); //获取超时时间的年月日时分秒 var year = timeOutDate.getFullYear(); var month = timeOutDate.getMonth()+1; var strDate = timeOutDate.getDate(); var hours = timeOutDate.getHours(); var minutes = timeOutDate.getMinutes(); var seconds = timeOutDate.getSeconds(); //转变成需要的日期格式。 var time = getCurrentTime(year,month,strDate,hours,minutes,seconds); console.log("超时时间="+time);//20170705130805 }
相关文章推荐
- 使用js时,如何获取系统当前时间并且得到格式为"yyyy年MM月"的日期
- js获取当前时间,返回字符格式
- JS获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS” 可以根据需求修改
- js 获取当前日期时间 格式为 yyyy-mm-dd hh:MM:ss
- js获取当前时间,并将标准日期格式转换成Long型
- 在js中获取当前时间并格式化为特定格式
- js获取当前时间 格式yyyy-MM-dd HH:MM:SS
- js 按照特定日期格式获取当前时间
- js 获取当前日期时间 格式为 yyyy-mm-dd hh:MM:ss
- js获取当前时间,日期格式为年月日
- js 获取当前日期时间 格式为 yyyy-mm-dd hh:MM:ss
- Js获取当前日期时间、比较及正则表达式转换格式
- js 获取当前时间 格式为 刷新成功2017-06-13 11:3:48
- js 字符串转时间格式,js 获取当前时间
- 关于几个js获取当前时间和处理返回数据改变时间格式的函数
- js中怎么获取系统当前时间 格式 yyyy-MM-dd HH:MM:SS
- js获取当前的日期时间 格式“yyyy-MM-dd HH:MM:SS”
- js 获取当前日期时间 格式为 yyyy-mm-dd hh:MM:ss
- JS快速获取当前时间减一小时、一天及日期格式处理
- js获取当前日期,并且转化为时间格式“yyyy-MM-dd HH:MM:SS”