JavaScript基础学习(二)--Date(日期)对象的学习

来源:互联网 发布:win10设置网络自动连接 编辑:程序博客网 时间:2024/05/14 03:41

总结关于date的方法:

Date():返回今天的日期和时间

getTime():计算从1970年的到今天又多少毫秒

setFullYear():设置具体的日期

toUTCString():把当前日期(根据UTC)转换为字符换   UTC与GMT含义完全相同

getDay():显示星期,不仅仅是数字

getHourse():返回对象的小时(0~23)

getMilliseconds():返回date对象的毫秒(0~999)

getMinutes():返回date的分钟数

getMonth():月份

getSeconds():秒数

getTimezoneOffset():返回本地时间与格林威治标准时间的分钟差

parse():范湖1970年1月1日午夜到指定日期的毫秒数

setDate():设置date对象中月的某一天

setFullYear()、setHours()、setMilliseconds()、setMinutes()、setMonth()、setSecondes()、

setTime():以毫秒设置Date对象

toISOString():使用ISO标准返回字符串的 日期格式

toJSON():以JSON数据格式返回日期格式

toLocaleDateString():根据本地时间格式,将Date对象的日期部分转换为字符串

toLocaleTimeString():据本地时间格式,将date对象的时间格式转换为字符串

toLocaleString():据本地时间格式,将Date对象转换为字符串

toString():将Date对象转换为字符串

toTimeString():将Date对象的时间部分转换为字符串




注意:GMT+0800 (中国标准时间)  GMT格林尼治平均时(Greenwich Mean Time)

GMT时间就是英国格林威治时间,也就是世界标准时间,是本初子午线上的地方时,是0时区的区时,与我国的标准时间北京时间(东八区)相差8小时,即晚8小时。
格林威治是英国伦敦泰晤士河南岸的一个地方,由于从19世纪开始,因为世界各国来往频繁,而欧洲大陆、美洲大陆和亚洲大陆都有各自的时区,所以为免混乱,各国的代表就在1884 年在美国华盛顿召开了国际大会,通过协议选出伦敦的格林威治,作为全球时间的中心点,格林威治标准时间因而诞生。所以有GMT功能的腕表就是说腕表拥有其中的小时表盘可以显示GMT时间。

date方法的举例应用:

<script>
var d=new Date();//返回今天的日期和时间
document.write(d);//返回Tue Nov 03 2015 10:48:34 GMT+0800 (中国标准时间)


function myFunction()
{
var d = new Date();
var x = document.getElementById("demo");
x.innerHTML=d.getTime();//计算从1970年的到今天又多少毫秒
}


function myFunction()
{
var d = new Date();
d.setFullYear(2020,10,3);//设置具体的日期

d.setDate(d.getDate()+5);//如果增加天数会改变月份或者年份,那么日期对象会自动完成这种转换。
var x = document.getElementById("demo");
x.innerHTML=d;
}


function myFunction()
{
var d = new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";


var x = document.getElementById("demo");
x.innerHTML=weekday[d.getDay()];//返回 Tuesday【今天星期二】
}

//制作一个钟表

<script>
function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
// add a zero in front of numbers<10
m=checkTime(m);//给小于10的数字前面添0,为了格式整齐
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout(function(){startTime()},500);//可以一秒一秒的改变时间
}
function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
  }
return i;
}
</script>


创建一个 Date 对象,调用对象的 myMet 方法:

<script>
Date.prototype.myMet=function()  //prototype:可以向对象添加属性和方法
{
if (this.getMonth()==0){this.myPro="January"};
if (this.getMonth()==1){this.myPro="February"};
if (this.getMonth()==2){this.myPro="March"};
if (this.getMonth()==3){this.myPro="April"};
if (this.getMonth()==4){this.myPro="May"};
if (this.getMonth()==5){this.myPro="June"};
if (this.getMonth()==6){this.myPro="July"};
if (this.getMonth()==7){this.myPro="August"};
if (this.getMonth()==8){this.myPro="Spetember"};
if (this.getMonth()==9){this.myPro="October"};
if (this.getMonth()==10){this.myPro="November"};
if (this.getMonth()==11){this.myPro="December"};
}
function myFunction()
{
var d = new Date();
d.myMet();
var x=document.getElementById("demo");
x.innerHTML=d.myPro;
}
</script>

0 0
原创粉丝点击