JS获取时间日期常用方法
1 当前时间:
new Date()
2 当前周:
function getCurrentWeek() {
var date = new Date()
var beginDate = new Date(date.getFullYear(), 0, 1);
var week = Math.ceil((parseInt((date - beginDate) / (24 * 60 * 60 * 1000)) + 1 + beginDate.getDay()) / 7);
return week;
}
3 当前月(获取的月份值范围为:0-11,0表示1月份):
new Date().getMonth()
4 当前年(注意与getYear的区别):
new Date().getFullYear()
5 当前星期几
function getWeekDate() {
var now = new Date();
var day = now.getDay();
var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
var week = weeks[day];
return week;
}
6 日期格式化
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;
}
使用方法:
new Date().format("yyyy-MM-dd hh:mm:ss");
7 一年有多少周
function getTotalWeek(year) {
// 一年第一天是周几
var first = new Date(year,0,1).getDay()
// 计算一年有多少天
if((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
var allyears = 366
}else {
var allyears = 365
}
// 计算一年有多少周
var week = parseInt((allyears + first) / 7)
if(((allyears + first) % 7) != 0) {
week += 1
}
return week
}
8 当月有多少天
function getCountDays() {
var curDate = new Date();
/* 获取当前月份 */
var curMonth = curDate.getMonth();
/* 生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
curDate.setMonth(curMonth + 1);
/* 将日期设置为0, 这里为什么要这样设置, 我不知道原因, 这是从网上学来的 */
curDate.setDate(0);
/* 返回当月的天数 */
return curDate.getDate();
}
持续更新中。。。
JS获取时间日期常用方法的更多相关文章
- js获取选中日期的当周的周一和周日
js获取选中日期的当周的周一和周日 第一种方法(推荐): function getWeekStr(str) { // 将字符串转为标准时间格式 str2 = Date.parse(str); let ...
- 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期
腾讯云图片鉴黄集成到C# 官方文档:https://cloud.tencent.com/document/product/641/12422 请求官方API及签名的生成代码如下: public c ...
- PHP获取时间日期的多种方法
分享下PHP获取时间日期的多种方法. <?php echo "今天:".date("Y-m-d")."<br>"; ...
- js获取时间相关函数
js获取时间函数 var myDate = new Date; var year = myDate.getFullYear();//获取当前年 var yue = myDate.getMonth()+ ...
- js获取时间加多山天和时间戳转换成日期
function huoqu(){ var data = $("#data").val();//获取的时间 var day = $('#day').val();//往后 ...
- js 获取时间比较全,留备用(zhuan)
var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位 ...
- js 获取时间 new Date()详细介绍
javaScript系列:js中获取时间new Date()详细介绍 (2012-03-31 09:54:25) 转载▼ 标签: js时间 new date() 字符类型 转换 分类: study-j ...
- 001 -js对时间日期的排序
001-JS对时间日期的排序 最近在做公司的项目时间,产品给了一个很简单的页面,让帮忙写一下.首先看一下产品的需求: 需要对该列表进行排序 思路:(1)可以在数据库写sql语句的时间直接一个DESC按 ...
- 一篇文章吃透iOS、JS的时间日期(Date, Calendar, Locale, TimeZone)
iOS 时间相关类 NSDate - 表示一个绝对的时间点. NSCalendar - 代表一个特定的日历,例如公历或者希伯来日历.它提供了一系列基于日期的计算,并且可以让你在"NSDate ...
随机推荐
- python 迭代器 iter多次消费
问题 Python 中的迭代器是我们经常使用的迭代工具, 但其只能消费一次,再次消费便会出现 StopIteration 报错. 解决方案 封装了一个类,当迭代器使用完后再次初始化. 代码 class ...
- K8S(09)交付实战-通过流水线构建dubbo服务
k8s交付实战-流水线构建dubbo服务 目录 k8s交付实战-流水线构建dubbo服务 1 jenkins流水线准备工作 1.1 参数构建要点 1.2 创建流水线 1.2.1 创建流水线 1.2.2 ...
- Django服务器布置(Ubuntu+uwsgi+nginx+Django)
一.安装Python apt install python3 二.安装pip apt install python3-pip 三.创建目录 创建虚拟服务目录 mkdir -p /data/env 创建 ...
- sscanf的最基础用法(非原创)
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 5 int main(){ 6 ch ...
- 【原】无脑操作:Centos 7.6 + MariaDB + Rsyslog + LogAnalyzer环境搭建
背景: 网络安全法第三章第二十一条明确规定"采取监测.记录网络运行状态.网络安全事件的技术措施,并按照规定留存相关的网络日志不少于六个月". 为了满足合规性的要求,应当建设相应的日 ...
- tensorflow加载ckpt出错
Issue链接 问题: tensorflow加载ckpt出错 此处原因: 该ckpt文件对应的tensorflow版本过老, 其中的部分内置变量名发生了改变. 提示: Key lstm_o/bidir ...
- Prettier All In One
Prettier All In One .prettierrc.js / .prettierrc / .prettierrc.json module.exports = { singleQuote: ...
- SwiftUI error All In One
SwiftUI error All In One Instance member xxx cannot be used on type yyy Instance member 'game' canno ...
- Swift private(set) All In One
Swift private(set) All In One SwiftUI Getters and Setters https://docs.swift.org/swift-book/Language ...
- js console.log color all in one
js console.log color all in one console.log color Chrome console.log 语法 / grammar %c, %s, css style ...