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

本文介绍了JavaScript中处理时间的方法,包括自定义函数实现时间和日期的格式化,使用moment.js库进行复杂的时间操作,以及通过构造函数扩展对象功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 PMAM PM
a小写的am pmam 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
XUnix时间戳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;}
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值