显示当时时间,面向对象prototype,constructor 属性

1.显示当时时间:

 

function dateDigitToString(num){
      return num < 10 ? '0' + num : num;
}
function get_time(){
    var currentDate = new Date(),
            year = dateDigitToString(currentDate.getFullYear()),
            month = dateDigitToString(currentDate.getMonth() + 1),
            date = dateDigitToString(currentDate.getDate()),
            hour = dateDigitToString(currentDate.getHours()),
            minute = dateDigitToString(currentDate.getMinutes()),
            second = dateDigitToString(currentDate.getSeconds()),
            formattedDateString = year + '年' + month + '月' + date + '日 ' + hour + ':' + minute + ':' + second;
    return formattedDateString;
 
}

 另外:

            moment.js不依赖任何第三方库,支持字符串、Date、时间戳以及数组等格式,
       可以像PHP的date()函数一样,格式化日期时间,计算相对时间,获取特定时间后的日期时间.

当前时间:

moment().format('YYYY-MM-DD HH:mm:ss'); //2014-09-24 23:36:09 

 今天星期几:

moment().format('d'); //3

 转换当前时间的Unix时间戳:

 

moment().format('X'); 

  相对时间:

 

7天后的日期:

 

moment().add('days',7).format('YYYY年MM月DD日'); //2014年10月01日 

9小时后的时间:

moment().add('hours',9).format('HH:mm:ss'); 

moment.js提供了丰富的说明文档,使用它还可以创建日历项目等复杂的日期时间应用。日常开发中最   常用的是格式化时间,下面是常用的格式成表格说明。

 

 

格式代码 说明 返回值例子
M 数字表示的月份,没有前导零 1到12
MM 数字表示的月份,有前导零 01到12
MMM 三个字母缩写表示的月份 Jan到Dec
MMMM 月份,完整的文本格式 January到December
Q 季度 1到4
D 月份中的第几天,没有前导零 1到31
DD 月份中的第几天,有前导零 01到31
d 星期中的第几天,数字表示 0到6,0表示周日,6表示周六
ddd 三个字母表示星期中的第几天 Sun到Sat
dddd 星期几,完整的星期文本 从Sunday到Saturday
w 年份中的第几周 如42:表示第42周
YYYY 四位数字完整表示的年份 如:2014 或 2000
YY 两位数字表示的年份 如:14 或 98
A 大写的AM PM AM PM
a 小写的am pm am pm
HH 小时,24小时制,有前导零 00到23
H 小时,24小时制,无前导零 0到23
hh 小时,12小时制,有前导零 00到12
h 小时,12小时制,无前导零 0到12
m 没有前导零的分钟数 0到59
mm 有前导零的分钟数 00到59
s 没有前导零的秒数 1到59
ss 有前导零的描述 01到59
X Unix时间戳 1411572969

 

2.prototype

prototype是一个针对于某一类的对象的方法,而且特殊的地方便在于:它是一个给类的对象添加

 

方法的方法。

javascript中的每个对象都有prototype属性,Javascript中对象的prototype属性是返回对象类型原型的引用

 

function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}
employee.prototype.year='35'
var bill=new employee("bob","Engineer",1985);
console.log(bill.year)
输出:35.

 3.constructor
属性返回对创建此对象的数组函数的引用。.
例1.展示如何使用 constructor 属性:
var test=new Array();

if (test.constructor==Array)
{
document.write("This is an Array");
}
if (test.constructor==Boolean)
{
document.write("This is a Boolean");
}
if (test.constructor==Date)
{
document.write("This is a Date");
}
if (test.constructor==String)
{
document.write("This is a String");
}
输出:This is an Array
 例2. 展示如何使用 constructor 属性:
function employee(name,job,born)
{
this.name=name;
this.job=job;
this.born=born;
}

var bill=new employee("Bill Gates","Engineer",1985);

document.write(bill.constructor);
 
输出:
function employee(name, job, born)
{this.name = name; this.job = job; this.born = born;}
 

猜你喜欢

转载自18633917479.iteye.com/blog/2356920