JS对时间的操作
JS时间操作大全
1、获取每个月的开始和结束。
2、获取每个季度的开始和结束。
3、获取当前季度。
4、把日期转换为字符串(支持各种格式)
...
5、未完待续,不断添加
String.prototype.padingStar=function(totalLength,padingStr="") {
if (totalLength <= this.length) {
return this;
}
var padLength = totalLength - this.length;
if (padLength <= padingStr.length) {
return padingStr.substring(0, padLength) + this;
} else {
var len = padLength / padingStr.length, n = 1,str='';
while (n<len) {
str += padingStr;
n++;
}
return str + padingStr.substring(0, padLength - (n-1) * padingStr.length) +this;
}
}
String.prototype.padingEnd = function (totalLength, padingStr="") {
//在开始的补全后面
if (totalLength <= this.length) {
return this;
}
var padLength = totalLength - this.length;
if (padLength <= padingStr.length) {
return padingStr.substring(0, padLength) + this;
} else {
var len = padLength / padingStr.length, n = 0,str='';
while (n<len) {
str += padingStr;
n++;
}
return this + padingStr.substring(0, padLength - (n - 1) * padingStr.length)+str;
}
}
//获取当前月的开始
Date.prototype.starOfMonth=function() {
return new Date(this.getFullYear(), this.getMonth(), 1, 00, 00, 00);
}
//获取当前月的结束
Date.prototype.endOfMonth=function() {
return new Date(this.getFullYear(), this.getMonth() + 1, 0, 23, 59, 59);
}
//获取当前季度的开始时间
Date.prototype.starofQuarter=function() {
return new Date(this.getFullYear(), (this.getQuarter() - 1) * 3, 01, 00, 00, 00);
}
//获取当前季度的结束时间
Date.prototype.endofQuarter=function() {
return new Date(this.getFullYear(), this.getQuarter() * 3-1 , 31, 23, 59, 59);
}
//获取当前时间对应的季度
Date.prototype.getQuarter=function() {
return Math.ceil((this.getMonth() + 1) / 3);
}
//获取当前时间对应的年的开始
Date.prototype.starOfYear=function() {
return new Date(this.getFullYear(), 01, 01, 00, 00, 00);
}
//获取当前时间对应年的结束
Date.prototype.endOfYear=function() {
return new Date(this.getFullYear(), 12, 31, 23, 59, 59);
}
//把时间格式化为字符串
Date.prototype.toDateString = function(format) {
if (typeof (format) == "undefined") {
return this.toString();
}
//可能我的第一个想法,就是
if (/y{4}/.test(format)) {
format = format.replace(/yyyy/g, this.getFullYear());
}
if (/y{2}/.test(format)) {
format = format.replace(/y{2}/g,this.getFullYear().toString().substr(2));
}
if (/M{2}/.test(format)) {
format = format.replace(/MM/,this.getMonth().toString().padingStar(2,0));
}
if (/dd/.test(format)) {
format = format.replace(/dd/,this.getDate().toString().padingStar(2,'0'));
}
if (/HH/.test(format)) {
format = format.replace(/HH/g, this.getHours().toString().padingStar(2, '0'));
}
if (/hh/.test(format)) {
format = format.replace(/hh/g, (hour < 12 ? hour : hour - 12).toString().padingStar(2, '0'));
}
if (/mm/.test(format)) {
format = format.replace(/mm/g, this.getMinutes().toString().padStart(2, '0'));
}
if (/ss/.test(format)) {
format = format.replace(/ss/g, this.getSeconds().toString().padStart(2, '0'));
}
return format;
}
//获取两个时间相隔的天数
Date.prototype.betweenDays=function(date) {
var daySpan = (Date.parse(this) - Date.parse(date)) / 86400000;
return daySpan;
}
github地址:https://github.com/gdoujkzz/JsDate.git
JS对时间的操作的更多相关文章
- js对时间的操作相关
摘自网络,我主要用了日期增加若干天之后获得的日期,就是现在是5月2号,我增加30天,应该返回6月几号来着,就是这个意思 用到了Date.prototype.DateAdd 方法,prototype的意 ...
- js中时间的操作
var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1 ...
- 关于JS的时间控制实现动态效果及实例操作
关于JS的时间控制 <script> BOM //Bowers Object Model 浏览器对象模型 setTimeout()// 延迟执行一次 ...
- js格式化时间和时间操作
js格式化时间 function formatDateTime(inputTime) { var date = new Date(inputTime); var y = date.getFullYea ...
- jsp+js完成用户一定时间未操作就跳到登录页面
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" ...
- js中常用的操作
1.js中常用的数组操作 2.js中常用的字符串操作 3.js中常用的时间日期操作 4.定时器
- js的时间操作方法
1.js获取系统时间格式为YYYY-MM-DD HH:MM:SS 1 function curDateTime(){ 2 var d = new Date(); 3 var year = d.getY ...
- Web页面长时间无操作后再获取焦点时转到登录界面
今天开始讲新浪博客搬到博客园. 在工作中遇到的小问题,感觉有点意思,就记录下来吧! 该问题分为两种情况,一.Web页面长时间无操作后,在对其进行操作,比如点击“首页”.“设 ...
- Python3 与 C# 面向对象之~继承与多态 Python3 与 C# 面向对象之~封装 Python3 与 NetCore 基础语法对比(Function专栏) [C#]C#时间日期操作 [C#]C#中字符串的操作 [ASP.NET]NTKO插件使用常见问题 我对C#的认知。
Python3 与 C# 面向对象之-继承与多态 文章汇总:https://www.cnblogs.com/dotnetcrazy/p/9160514.html 目录: 2.继承 ¶ 2.1.单继 ...
随机推荐
- CCF-201403-2-窗口
问题描述 试题编号: 201403-2 试题名称: 窗口 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平 ...
- linux API函数大全
获取当前执行路径:getcwd1. API之网络函数 WNetAddConnection 创建同一个网络资源的永久性连接 WNetAddConnection2 创建同一个网络资源的连接 WNetAdd ...
- Simple prefix compression
题目 看懂题目的意思 直接模拟就能够了 好像不用递归就能够了. . 尽管这周学的是递归 还是偷了一些懒 直接模拟 在说这个题目的意思 本来能够写的非常清楚的下标 题目非要把两个字符串的表示方法写的这 ...
- com.sun.mail.smtp.SMTPSendFailedException: 553 Mail from must equal authorized user
1.错误描写叙述 553 Mail from must equal authorized user com.sun.mail.smtp.SMTPSendFailedException: 553 Mai ...
- ITM事件直接接收并解析
之前在实施一个监控项目时.客户由于买了IBM的小机.当前就赠送了TIVOLI的系统监控软件一套,客户也在他们的生产环境中部署了ITM的监控.由于没有购买IBM的netcool,无法集中管理告警事件,请 ...
- JAVA入门[3]—Spring依赖注入
Spring支持属性注入和构造器注入,它支持XML和注解两种方式.本文介绍Spring控制反转容器加载包含beans的XML文件,实现依赖注入. 一.创建bean实例 暂且抛开对象依赖,我们先看下如何 ...
- Swift 是猴还是猿?
欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 作者:段义鹏 导语 Swift和Objective-C是目前开发 Apple App的两门主要语言.Swift自2014年发布到目前为止其行业 ...
- windows 下使用VMware Workstation Pro 工具,ubuntu创建虚拟机
本文记录windows 下使用VMware Workstation Pro 工具,ubuntu创建虚拟机 的步骤 第一步 [文件] --- [新建虚拟机] 第二步 弹出的新建虚拟机向导对话框 标准 ...
- CRL快速开发框架升级到4.52,谈谈开发过程中的优化
CRL4.5版本已经稳定使用于目前的几个中型项目中,在实际使用中,也发现了不少问题,这些问题都在4.52中提交 CRL具体功能和使用请浏览 CRL快速开发框架系列教程 由于现在项目是一套业务系统,查询 ...
- 在Windows上运行Spark程序
一.下载Saprk程序 https://d3kbcqa49mib13.cloudfront.net/spark-2.1.1-bin-hadoop2.7.tgz 解压到d:\spark-2.1.1-bi ...