因为项目开发中遇到需要向后台传本周的开始和结束时间,以及上一周的起止时间,就琢磨了半天,总算写出来一套,写篇文章是为了方便自己记忆,也是分享给需要的人,水平有限,写的不好请见谅:

  • getDateStr3函数是为了把时间对象转变为yy-mm-dd的字符串,方便传值;

  • getWeekStartAndEnd函数是获取周的起止时间,并且用getDateStr3转换成字符串放到数组中,其中参数0代表当前周,-1代表前一周,-2代表上上周,以此类推,反过来也可以1代表下一周;

  • getMonthStartAndEnd函数是获取月的起止时间,传参同上

//获取当前日期yy-mm-dd
//date 为时间对象
function getDateStr3(date) {
var year = "";
var month = "";
var day = "";
var now = date;
year = ""+now.getFullYear();
if((now.getMonth()+1)<10){
month = "0"+(now.getMonth()+1);
}else{
month = ""+(now.getMonth()+1);
}
if((now.getDate())<10){
day = "0"+(now.getDate());
}else{
day = ""+(now.getDate());
}
return year+"-"+month+"-"+day;
}
/**
* 获得相对当前周AddWeekCount个周的起止日期
* AddWeekCount为0代表当前周 为-1代表上一个周 为1代表下一个周以此类推
* **/
function getWeekStartAndEnd(AddWeekCount) {
//起止日期数组
var startStop = new Array();
//一天的毫秒数
var millisecond = 1000 * 60 * 60 * 24;
//获取当前时间
var currentDate = new Date();
//相对于当前日期AddWeekCount个周的日期
currentDate = new Date(currentDate.getTime() + (millisecond * 7*AddWeekCount));
//返回date是一周中的某一天
var week = currentDate.getDay();
//返回date是一个月中的某一天
var month = currentDate.getDate();
//减去的天数
var minusDay = week != 0 ? week - 1 : 6;
//获得当前周的第一天
var currentWeekFirstDay = new Date(currentDate.getTime() - (millisecond * minusDay));
//获得当前周的最后一天
var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6));
//添加至数组
startStop.push(getDateStr3(currentWeekFirstDay));
startStop.push(getDateStr3(currentWeekLastDay)); return startStop;
}
/**
* 获得相对当月AddMonthCount个月的起止日期
* AddMonthCount为0 代表当月 为-1代表上一个月 为1代表下一个月 以此类推
* ***/
function getMonthStartAndEnd(AddMonthCount) {
//起止日期数组
var startStop = new Array();
//获取当前时间
var currentDate = new Date();
var month=currentDate.getMonth()+AddMonthCount;
if(month<0){
var n = parseInt((-month)/12);
month += n*12;
currentDate.setFullYear(currentDate.getFullYear()-n);
}
currentDate = new Date(currentDate.setMonth(month));
//获得当前月份0-11
var currentMonth = currentDate.getMonth();
//获得当前年份4位年
var currentYear = currentDate.getFullYear();
//获得上一个月的第一天
var currentMonthFirstDay = new Date(currentYear, currentMonth,1);
//获得上一月的最后一天
var currentMonthLastDay = new Date(currentYear, currentMonth+1, 0);
//添加至数组
startStop.push(getDateStr3(currentMonthFirstDay));
startStop.push(getDateStr3(currentMonthLastDay));
//返回
return startStop;
}
 

js 计算月/周的第一天和最后一天的更多相关文章

  1. 【iOS】得到当前年、月、周的第一天和最后一天

    在写一个记账软件,其中有个统计功能.比如,统计某月的支出,需要知道某天所在的月的第一天和最后一天,以便从数据库中根据时间取数据. 话不多说,上代码: // // EBDate.h // ChargeM ...

  2. C#获取周的第一天、最后一天、月第一天和最后一天

    [csharp] view plaincopyprint? public class DateTimeTool { /// <summary> /// 获取指定日期所在周的第一天,星期天为 ...

  3. 【转载】对C#DateTime的一些扩展,计算周内第一天,最后一天

    /// <summary> /// DateTime的一些扩展 /// </summary> public class DateTime2 { /// <summary& ...

  4. c# 获取某日期所在周的第一天和最后一天

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WyfC ...

  5. c# 获取某日期所在周的第一天和最后一天(转)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WyfC ...

  6. 用js获取周、月第一天和最后一天(转载)

    var getCurrentWeek = function (day) { var days = ["周日", "周一", "周二", &q ...

  7. C# DateTime 月第一天和最后一天 取法

    取得某月和上个月第一天和最后一天的方法 /// <summary> /// 取得某月的第一天 /// </summary> /// <param name="d ...

  8. java获取当月的第一天和最后一天,获取本周的第一天和最后一天

    /** * 获取指定日期所在周的第一天和最后一天,用下划线连接 * @param dataStr * @return * @throws ParseException */ public static ...

  9. 在oracle里,如何取得本周、本月、本季度、本年度的第一天和最后一天的时间

    在oracle里,如何取得本周.本月.本季度.本年度的第一天和最后一天的时间 --本周 select trunc(sysdate,'d')+1 from dual; select trunc(sysd ...

随机推荐

  1. SQLSERVER设置行号

    select row_number()over(order by columnname)as rownum,* from tablename 按照columnname列进行排列

  2. Linux 分区和目录

    [1. 分区与目录概念理解]  Linux的分区是物理上的概念,就像我们把一块硬盘分成C:,D:,E:三个区一样,物理上将存储空间分开 Linux的目录是逻辑上的概念,Linux的目录树实际上是一个分 ...

  3. [iOS Animation]-CALayer 变换

    变换 很不幸,没人能告诉你母体是什么,你只能自己体会 -- 骇客帝国 在第四章“可视效果”中,我们研究了一些增强图层和它的内容显示效果的一些技术,在这一章中,我们将要研究可以用来对图层旋转,摆放或者扭 ...

  4. PHP 判断几秒前,几分钟,几小时前

    PHP 对于时间的过了多久的判断,几秒前,几分钟前,几小时前,$time = strtotime("2017-01-15 14:42:00"); $time = strtotime ...

  5. UVA - 208 Firetruck(消防车)(并查集+回溯)

    题意:输入着火点n,求结点1到结点n的所有路径,按字典序输出,要求结点不能重复经过. 分析:用并查集事先判断结点1是否可以到达结点k,否则会超时.dfs即可. #pragma comment(link ...

  6. JDBC操作数据时中文乱码

    /** * DB地址 */ private static final String DB_URL="jdbc:mysql://localhost:3306/db_book?useUnicod ...

  7. android UI线程安全问题

    在Android的子线程去更新UI的内容,会导致不确定的异常. 因为Android有个模式是,单一线程模型:Android UI工具箱(toolkit)不是一个线程安全的,并且它总是被放在主线程上操作 ...

  8. Spring与Struts框架整合

    Spring与Struts框架整合 Struts,用Action处理请求 Hibernate,操作数据库 Spring,负责对象创建 Spring与Struts框架整合的关键点在与:让Struts框架 ...

  9. 那些年我们一起改过的bug

    ORA-01861: 文字与格式字符串不匹配 ORA-00936: 缺失表达式 ORA-01810 格式代码出现两次 ORA-01722: 无效数字 无效的列索引

  10. ios framework 开发

    ios framework 开发 之 参考 ios framework 开发 之 实战 iOS workspace 依次编译多个工程