js处理时间(一)
2016-09-09 16:05
330 查看
最近正好闲下来了!整理了一下时间处理的方法,都是自己写的哈!!
本篇封装:
(1) js取得2016-08-11 19:46:01格式的时间
(2) js取得本周第一天/最后一天
(3) js取得本月第一天/最后一天
<script type="text/javascript">
/* 输出2016-08-11 19:46:01的格式 */
var today = new Date()
//昨天:先算出昨天的日期,用setDate方法把今天日期减1
today.setDate(today.getDate() - 1)
console.log(today)
//向 1970/01/01 添加 77771564221 毫秒
//today.setTime(77771564221)
//设置为月初第一天
today.setDate(1)
year = today.getFullYear()
month = today.getMonth().toString().length == 1?'0'+(today.getMonth()+1):today.getMonth()+1
dateString = today.getDate()<=9?'0'+today.getDate():today.getDate()
hour = today.getHours()<=9?'0'+today.getHours():today.getHours()
min = today.getMinutes()<=9?'0'+today.getMinutes():today.getMinutes()
sec = today.getSeconds()<=9?'0'+today.getSeconds():today.getSeconds()
dateFormat = year+'-'+month+'-'+dateString + ' ' + hour + ':' + min + ':' + sec
console.log(dateFormat)
/* 月份 星期第一天测试
var today = new Date(), msg = [];
today.setDate(12)
var todayDate = today.getDate()
var todayWeekDay = today.getDay()
*/
/*week开始的第一天
today.setDate(todayDate-todayWeekDay) //此处的today是当周的第一天
weekFirstDay = today.getFullYear().toString() +"-"+ (today.getMonth()+1) +"-"+ today.getDate()
weekFirstDate = today.getDate() //获取当周第一天的日期
alert(weekFirstDay)
today.setDate(weekFirstDate+6) //此处的today是当周第六天 即最后一天j
weekLastDay = today.getFullYear().toString() +"-"+ (today.getMonth()+1) +"-"+ today.getDate()
alert(weekLastDay)
*/
/* month 第一天
today.setDate(1) //此处的today是当月的第一天
monthFirstDay = today.getFullYear().toString() +"-"+ (today.getMonth()+1) +"-"+ (today.getDate())
alert(monthFirstDay)
today.setMonth(today.getMonth()+1) //此处的today是下个月的第一天
today.setDate(0) //此处的today是当月的最后一天
monthLastDay = today.getFullYear().toString() +"-"+ (today.getMonth()+1) +"-"+ (today.getDate())
alert(monthLastDay)
*/
</script>
本篇封装:
(1) js取得2016-08-11 19:46:01格式的时间
(2) js取得本周第一天/最后一天
(3) js取得本月第一天/最后一天
<script type="text/javascript">
/* 输出2016-08-11 19:46:01的格式 */
var today = new Date()
//昨天:先算出昨天的日期,用setDate方法把今天日期减1
today.setDate(today.getDate() - 1)
console.log(today)
//向 1970/01/01 添加 77771564221 毫秒
//today.setTime(77771564221)
//设置为月初第一天
today.setDate(1)
year = today.getFullYear()
month = today.getMonth().toString().length == 1?'0'+(today.getMonth()+1):today.getMonth()+1
dateString = today.getDate()<=9?'0'+today.getDate():today.getDate()
hour = today.getHours()<=9?'0'+today.getHours():today.getHours()
min = today.getMinutes()<=9?'0'+today.getMinutes():today.getMinutes()
sec = today.getSeconds()<=9?'0'+today.getSeconds():today.getSeconds()
dateFormat = year+'-'+month+'-'+dateString + ' ' + hour + ':' + min + ':' + sec
console.log(dateFormat)
/* 月份 星期第一天测试
var today = new Date(), msg = [];
today.setDate(12)
var todayDate = today.getDate()
var todayWeekDay = today.getDay()
*/
/*week开始的第一天
today.setDate(todayDate-todayWeekDay) //此处的today是当周的第一天
weekFirstDay = today.getFullYear().toString() +"-"+ (today.getMonth()+1) +"-"+ today.getDate()
weekFirstDate = today.getDate() //获取当周第一天的日期
alert(weekFirstDay)
today.setDate(weekFirstDate+6) //此处的today是当周第六天 即最后一天j
weekLastDay = today.getFullYear().toString() +"-"+ (today.getMonth()+1) +"-"+ today.getDate()
alert(weekLastDay)
*/
/* month 第一天
today.setDate(1) //此处的today是当月的第一天
monthFirstDay = today.getFullYear().toString() +"-"+ (today.getMonth()+1) +"-"+ (today.getDate())
alert(monthFirstDay)
today.setMonth(today.getMonth()+1) //此处的today是下个月的第一天
today.setDate(0) //此处的today是当月的最后一天
monthLastDay = today.getFullYear().toString() +"-"+ (today.getMonth()+1) +"-"+ (today.getDate())
alert(monthLastDay)
*/
</script>
相关文章推荐
- jsp用cookie保存登陆信息
- JavaScript 随机链接
- 如何使用js动态创建table,并隔行换色
- JavaScript 使用反斜杠对代码行进行折行
- ELK之LogStash读取JSON日志分类型建立索引
- Javascript基础
- JavaScript实现输入框与清空按钮联动效果
- 将Json数据中的“字符串形式”的数组转为数组形式
- Three.js快速入门教程
- jsp引入js时的绝对路径和相对路径
- 在前台接收jsonp数据(练习)
- 用c写一个简单json处理器之具体实现,应该可能会不断完善吧(三)
- 用c写一个简单json处理器之头文件,可能会不断完善吧(二)
- js数组与字符串的相互转换方法 数组常用的方法
- 用c写一个简单json处理器(一)
- jsp页面onclick事件
- 用c写一个简单json处理器
- 百度地图中动态生成网格
- 实现不同js中方法相互调用
- 解决使用Js赋值在浏览器上显示乱码问题