JS获取系统的指定定年月日

 1 /**
 2  * 获取系统当前时间
 3  */
 4 function getNowYearMouth(){
 5      var date=new Date;
 6      var nowYearMouth=date.getMonth()+1;
 7      var year=date.getFullYear();
 8       var day = date.getDate();
 9      nowYearMouth =(nowYearMouth<10 ? "0"+nowYearMouth:nowYearMouth);    //获取当前时间----月份
10      var nowYearMouthCur = (year.toString()+"-"+nowYearMouth.toString());  //获取当前时间----年月
11      var beforsixYearMouthday= getBeforeYearMouthDay();
12      return  nowYearMouthCur;
13 }
14
15 /**
16  * 获取系统当前年月日
17  */
18 function getNowYearMouthDay(){
19      var date=new Date;
20      var nowYearMouth=date.getMonth()+1;
21      var year=date.getFullYear();
22       var day = date.getDate();
23      nowYearMouth =(nowYearMouth<10 ? "0"+nowYearMouth:nowYearMouth);    //获取当前时间----月份
24      day=(day<10?"0"+day:day);
25      var nowYearMouthDay = (year.toString()+"-"+nowYearMouth.toString()+"-"+day.toString());//获取系统当前的年月日
26      return nowYearMouthDay;
27 }
28
29
30
31 /**
32  * 获取系统前六个月时间--年月
33  */
34 function getbeforeYearMouth()  {
35      var date=new Date;
36 //     date = new Date( date - 86400000 * 30 * 10 );
37      var month=date.getMonth()+1;
38      var year=date.getFullYear();
39      for (var i=0;i<6;i++)
40      {
41          if(month<=0)
42          {
43              month = month+12;
44              year--;
45          }
46          var tempmonth =(month<10 ? "0"+month:month);
47          var mydate = year.toString()+"-"+tempmonth.toString();
48          month--;
49      }
50      return mydate;
51 }
52 /**
53  * 获取系统前六个月的年月日
54  */
55 function getBeforeYearMouthDay(){
56     var tempmonth;
57     var date=new Date;
58     var month=date.getMonth()+1;
59     var year=date.getFullYear();
60     var day = date.getDate();
61     for (var i=0;i<=12;i++){
62         if(month<=0) {
63             month = month+12;
64             year--;
65         }
66         tempmonth =(month<10 ? "0"+month:month);
67       month--;
68     }
69     if((year%100!=0)&(year%400==0)&&(year%4==0)){//是闰年的情况下
70             if(tempmonth==2){  //若果是2月份
71                 if(day>29){
72                     tempmonth = tempmonth+1;
73                 }else{
74                     tempmonth = tempmonth;
75                 }
76                 }else{//不是2月份的情况
77                     tempmonth = tempmonth;
78                 }
79             }else{//不是闰年的情况下
80                 if(tempmonth==2){  //若果是2月份
81                     if(day>28){
82                         tempmonth = tempmonth+1;
83                     }else{
84                         tempmonth = tempmonth;
85                     }
86                 }else{//不是2月份的情况
87                         tempmonth = tempmonth;
88                     }
89             }
90     if(day<10){
91         day="0"+day;
92     }
93    var beforeYearMouthDay = year.toString()+"-"+tempmonth.toString()+"-"+day.toString();
94    return beforeYearMouthDay;
95 }
时间: 2024-08-01 22:46:50

JS获取系统的指定定年月日的相关文章

js获取系统月份大小总是小一位

一 问题:项目过程中用一下方法获取系统日期,结果月份总是小一位. var myDate = new Date();myDate.getFullYear();myDate.getMonth();$("#date1").val(myDate.getFullYear() + '/' + myDate.getMonth()); 二  解决方法  查询相关资料后得知JS获取系统月份getMonth() 是0-11来表示1-12的.最后将项目源码最后行改成 $("#date1"

js 获取系统当前时间

JS获取当前的日期和时间的方法:var myDate = new Date();myDate.getYear(); //获取当前年份(2位)myDate.getFullYear(); //获取完整的年份(4位,1970-????)myDate.getMonth(); //获取当前月份(0-11,0代表1月)myDate.getDate(); //获取当前日(1-31)myDate.getDay(); //获取当前星期X(0-6,0代表星期天)myDate.getTime(); //获取当前时间(

js 获取系统字体

参考自网上,非原创. 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 </head> 6 <body> 7 <div style="left: 0px; positon: absolute; top: 0px;

原生js获取系统时间

关于时间日常项目中可能会有很多需求,今天就写一个最简单的,将系统时间写入到页面上.直接来看js代码: <script> window.onload=function(){ var Body=document.body; //把获取出的时间封装为一个函数,方便加定时器让其自动循环 function Time(){ var str=""; var myTime=new Date(); var iYear = myTime.getFullYear(); var iMonth =

【转】通过js获取系统版本以及浏览器版本

1 function getOsInfo() { 2 var userAgent = navigator.userAgent.toLowerCase(); 3 var name = 'Unknown'; 4 var version = "Unknown"; 5 if(userAgent.indexOf("win") > -1) { 6 name = "Windows"; 7 if(userAgent.indexOf("window

js获取URL中指定的值

function getSearchString(key) { // 获取URL中?之后的字符 var str = location.search; str = str.substring(1,str.length); // 以&分隔字符串,获得类似name=xiaoli这样的元素数组 var arr = str.split("&"); var obj = new Object(); // 将每一个数组元素以=分隔并赋给obj对象 for(var i = 0; i &l

便利--获取系统的时分秒,年月日

在项目开发的时候我们经常会使用到时间,如何快速获得现在的时间: 源代码: LGlobalNormalTime.h 与 LGlobalNormalTime.m 1 // Created by Lisa on 14-10-14. 2 // Copyright (c) 2014年 Lisa. All rights reserved. 3 // 4 5 #import <Foundation/Foundation.h> 6 7 @interface LGlobalNormalTime : NSObje

js获取url中指定参数的值(含带hash)

function getUrlVars() { var vars = {}; var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) { vars[key] = value; } ); return vars; } 使用如: http://localhost?name=xxg getUrlVars()["name"] 返回的值为 xx

js获取一个字符串中指定字符串第n次出现的位置

1.JS获取一个字符串中指定字符串第n次出现的位置 了解类似的获取字符位置的方法: 1.1 charAt() 获取字符串指定位置的字符 用法:strObj是字符串对象,index是指定的位置,(位置从0开始数) strObj.charAt(index) 1.2 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置 用法:stringObject是字符串对象,searchvalue是指定的字符串值,fromindex(可有可无)指定开始匹配字符串值的位置,若无,表示从0位置开始