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.单继 ...
随机推荐
- 大白话Vue源码系列(01):万事开头难
阅读目录 Vue 的源码目录结构 预备知识 先捡软的捏 Angular 是 Google 亲儿子,React 是 Facebook 小正太,那咱为啥偏偏选择了 Vue 下手,一句话,Vue 是咱见过的 ...
- models中的pk主键用法
class FrontUserModel(models.Model): uid = models.UUIDField(primary_key=True,default=uuid.uuid4) emai ...
- javaweb学习总结(七)——HttpServletResponse对象(一)(转)
转载自 http://www.cnblogs.com/xdp-gacl/p/3789624.html Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对 ...
- Nexys3学习手记1:写在前面的话
偶然的机会,结识了xilinx的几位大牛,便毫不客气的从他们的手中接过了基于Spartan-6的由Digilent公司研发的Nexys3开发板(如图1所看到的).记得非常久非常久曾经初识FPGA的时候 ...
- Caffe-5.2-(GPU完整流程)训练(依据googlenet微调)
上一篇使用caffenet的模型微调.但由于caffenet有220M太大,測试速度太慢.因此换为googlenet. 1. 训练 迭代了2800次时死机,大概20分钟. 使用的是2000次的模型. ...
- 《Linux Device Drivers》第十八章 TTY驱动程序——note
简单介绍 tty设备的名称是从过去的电传打字机缩写而来,最初是指连接到Unix系统上的物理或虚拟终端 Linux tty驱动程序的核心紧挨在标准字符设备驱动层之下,并提供了一系列的功能,作为接口被终端 ...
- JS 循环遍历JSON数据 分类: JS技术 JS JQuery 2010-12-01 13:56 43646人阅读 评论(5) 收藏 举报 jsonc JSON数据如:{"options":"[{
JS 循环遍历JSON数据 分类: JS技术 JS JQuery2010-12-01 13:56 43646人阅读 评论(5) 收藏 举报 jsonc JSON数据如:{"options&q ...
- 以pfile或者spfile启动时show parameter pfile的不同结果
普通启动: SQL> show parameter pfile NAME TYPE VALUE ------------------------------------ ----------- ...
- ajaxSetup设置Ajax请求的默认值
ajaxSetup() 方法为将来的 AJAX 请求设置默认值.语法$.ajaxSetup({name:value, name:value, ... }) 该参数为带有一个或多个名称/值对的 AJAX ...
- 学习customEvent
title: 认真学习customEvent tags: DOM date: 2017-7-22 23:20:57 --- 最近要实现一个模拟的select元素组件,所以好好看了这个自定义事件api, ...