时间 new Date()

  获取时间,可以理解为是系统默认的函数。
从小括号里面获取系统时间日期,相当于在调用系统默认的函数。
年 getFullYear() 注意,有Full。
月 getMonth() 注意,系统是从0而不是人1开始。
日 getDate() 注意,是Date不是day。
周 getDay() 注意,这个才是day。
时 getHours()
分 getMinutes()
秒 getSeconds()
var mydate=new Date();
console.log(mydate) //Fri Dec 09 2016 13:58:48 GMT+0800 (中国标准时间)
//2016年9号 13点48分
//使用的是 GMT 格式的时候
//下面获取 (get) 年份
var iYear=mydate.getFullYear();
console.log(iYear) //2016 年
var iMonth=mydate.getMonth();
console.log(iMonth) //11, 月份,系统使用的月份是从0开始的,范围是0到11。
console.log(iMonth+1) //12, 月份,用户使用的月份
var iDay=mydate.getDate(); //天是Date不是day。
console.log(iDay) //9,
var iWeek=mydate.getDay(); //星期才是day
console.log(iWeek) //5
var iHours=mydate.getHours(); //小时
console.log(iHours) //14
var iMin=mydate.getMinutes(); //分钟
console.log(iMin) //13
var iSec=mydate.getSeconds(); //秒
console.log(iSec) //33

每秒更新的时钟

setInterval(function(){
  var mydate=new Date();
  var mydate2=(mydate.getFullYear()+"年 "+
    (mydate.getMonth()+1)+"月 "+
    mydate.getDate()+"日 "+
    " 星期"+mydate.getDay()+" "+
    mydate.getHours()+"时 "+
    mydate.getMinutes()+"分 "+
    mydate.getSeconds()+"秒 ");
    document.getElementsByTagName("body")[0].innerHTML=mydate2;
},1000)

点击显示每秒更新的时间,补0

document.write('<input type="button" value="显示时间" ><input>');
var ipt=document.getElementsByTagName("input");
var i_h;
var i_m;
var i_s;
ipt[0].onclick=function(){
  function time_box(){ //时间获取函数
    var i_time=new Date();
    if(i_time.getHours()<10){ //如果小于10,补上0
      i_h="0"+i_time.getHours();
    }else{
      i_h=i_time.getHours();
    }
    if(i_time.getMinutes()<10){
      i_m="0"+i_time.getMinutes();
    }else{
      i_m=i_time.getMinutes();
    }
    if(i_time.getSeconds()<10){
      i_s="0"+i_time.getSeconds();
    }else{
      i_s=i_time.getSeconds();
    }
    ipt[1].value=i_h + " : " + i_m + " : " + i_s; //改变输入框里的值
  }
  setInterval(time_box,1000); //定时器,每秒执行
}
  也可以自己写个补0函数
function orz(a){
  if(a<10){
    return "0"+a
  }else{
    return a
  }
}

 

js时间函数的更多相关文章

  1. js获取当前时间,js时间函数

    Js获取当前日期时间及其它操作,js时间函数 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); ...

  2. js 时间函数 及相关运算大全

    js 时间函数 及相关运算大全 var myDate = new Date(); myDate.getYear();        //获取当前年份(2位) myDate.getFullYear(); ...

  3. js 时间函数封装

    html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  4. JS 时间函数 / 格式化时间戳

    处理时间主要使用时间对象 Date , 其提供两个静态方法 Date.now() //获得当前时间戳 Date.parse() //将字符串转化成时间戳 创建对象 new Date(); // 返回当 ...

  5. js时间格式化函数,支持Unix时间戳

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. date时间函数

    时间函数: date();和time();的相互转换 time();   在PHP中单位是秒,在js中是毫秒. microtime();  毫秒 date('Y-m-d H:i:s',time()); ...

  7. js 时间处理

    1.格式化时间 function GetDateTimeFormatter(value) {        if (value == undefined) {            return &q ...

  8. js部分---函数与递归;

    function (){}//匿名函数 1.function hanshu () { alert("这是我第一个函数"); } hanshu();//调用函数 2.//有参数的函数 ...

  9. JS中函数的基础知识

    函数 一.  函数定义 函数又叫方法,在程序里面函数是用来执行某些特定功能的代码.为了减少重复使用代码,可以把特定功能的代码做成函数,需要使用时拿出来调用.alert();就是一个很常见的.简单的函数 ...

随机推荐

  1. .net 更新数据 ado.net parameter

    UPDATE yborder_ordernotes SET recoder400= @FileAddress, havefile400 = 1 WHERE id = @OrderID Maticsof ...

  2. POJ 2155 Matrix (二维线段树)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226   Accepted: 6461 Descripti ...

  3. NSMutableAttributedString(改变文字颜色)

    //类型 //创建一个label    UILabel *label1=[[UILabel alloc]initWithFrame:CGRectMake(130, 60,250, 150)];     ...

  4. unity3d - new 不出的单例

    可能习惯了写单例的朋友,或者常规的单例模式 会这样做 private static Single instance; public static Single Instance(){ if (inst ...

  5. eclipse新建项目,报错“Error: workspace\appcompat_v7\res\values-v21\styles_base.xml No resource found that matches the given name”

    新建项目报错,不知道为什么,以前从未出现过的错误,把sdk更新之后,出现莫名错误,自己也是一知半解,在网上找了好久的错误,终于在一个english网站找到了解决方法,soga,从未觉得english如 ...

  6. JS中delete删除对象属性

    1.删除对象属性 function fun(){   this.name = 'mm';   }   var obj = new fun();   console.log(obj.name);//mm ...

  7. jQuery阻止默认行为和阻止冒泡

    1.阻止默认行为:通常是值一个标签的默认行为,如button的提交表单,a标签的跳转等. 那如何阻止标签的默认行为? 1)return false 2) e.preventDefault(); < ...

  8. Mac上Homebrew的使用 (Homebrew 使 OS X 更完整)

    0 Homebrew是啥? “Homebrew installs the stuff you need that Apple didn’t.——Homebrew 使 OS X 更完整”. Homebr ...

  9. Codeforces #259 Div.2

    A. Little Pony and Crystal Mine 模拟题. 用矩阵直接构造或者直接根据关系输出 B. Little Pony and Sort by Shift 模拟题. 通过提供的操作 ...

  10. 6.7 Binder机制

    Binder在Android系统中江湖地位非常之高.在Zygote孵化出system_server进程后,在system_server进程中出初始化支持整个Android framework的各种各样 ...