小程序date时间

date

语法

生成 date 对象需要使用 getDate函数, 返回一个当前时间的对象。

getDate()

getDate(milliseconds)

getDate(datestring)

getDate(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]])

参数

milliseconds: 从1970年1月1日00:00:00 UTC开始计算的毫秒数

datestring: 日期字符串,其格式为:"month day, year hours:minutes:seconds"

示例代码:

var date = getDate(); //返回当前时间对象

date = getDate(1500000000000);

// Fri Jul 14 2017 10:40:00 GMT+0800 (中国标准时间)

date = getDate('2017-7-14');

// Fri Jul 14 2017 00:00:00 GMT+0800 (中国标准时间)

date = getDate(2017, 6, 14, 10, 40, 0, 0);

// Fri Jul 14 2017 10:40:00 GMT+0800 (中国标准时间)

属性

constructor:返回字符串 “Date”。

方法

toString() 方法返回一个字符串,以本地的时区表示该 Date 对象。

const event = new Date('August 19, 1975 23:15:30');

console.log(event.toString());

// expected output: Tue Aug 19 1975 23:15:30 GMT+0200 (CEST)

// (注意:您的时区可能会有所不同)

toDateString() 方法以美式英语和人类易读的形式返回一个日期对象日期部分的字符串。

const event = new Date(1993, 6, 28, 14, 39, 7);

console.log(event.toString());

// expected output: Wed Jul 28 1993 14:39:07 GMT+0200 (CEST)

// (注意:您的时区可能会有所不同)

console.log(event.toDateString());

// expected output: Wed Jul 28 1993

toTimeString() 方法以人类易读形式返回一个日期对象时间部分的字符串,该字符串以美式英语格式化。

const event = new Date('August 19, 1975 23:15:30');

console.log(event.toTimeString());

// expected output: 23:15:30 GMT+0200 (CEST)

// (注意:您的时区可能会有所不同)

toLocaleString() 方法返回该日期对象的字符串,该字符串格式因不同语言而不同。新增的参数 locales 和 options 使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)。

const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

// 英国英语使用日、月、年顺序和24小时时间,不包括上午/下午

console.log(event.toLocaleString('en-GB', { timeZone: 'UTC' }));

// expected output: 20/12/2012, 03:00:00

// 韩国语使用年、月、日顺序和12小时时间,包括上午/下午

console.log(event.toLocaleString('ko-KR', { timeZone: 'UTC' }));

// expected output: 2012. 12. 20. 오전 3:00:00

toLocaleDateString() 方法返回该日期对象日期部分的字符串,该字符串格式因不同语言而不同。新增的参数 locales 和 options 使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)

const event = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };

console.log(event.toLocaleDateString('de-DE', options));

// expected output (根据当地时区变化): Donnerstag, 20. Dezember 2012

console.log(event.toLocaleDateString('ar-EG', options));

// expected output (根据当地时区变化): الخميس، ٢٠ ديسمبر، ٢٠١٢

console.log(event.toLocaleDateString(undefined, options));

// expected output (根据本地时区和默认区域设置而变化): Thursday, December 20, 2012

 toLocaleTimeString() 方法返回该日期对象时间部分的字符串,该字符串格式因不同语言而不同。新增的参数 locales 和 options 使程序能够指定使用哪种语言格式化规则,允许定制该方法的表现(behavior)。

// 根据时区,您的结果会有所不同

const event = new Date('August 19, 1975 23:15:30 GMT+00:00');

console.log(event.toLocaleTimeString('en-US'));

// expected output: 1:15:30 AM

console.log(event.toLocaleTimeString('it-IT'));

// expected output: 01:15:30

console.log(event.toLocaleTimeString('ar-EG'));

// expected output: ١٢:١٥:٣٠ ص

valueOf() 方法返回一个 Date 对象的原始值

const date1 = new Date(Date.UTC(96, 1, 2, 3, 4, 5));

console.log(date1.valueOf());

// expected output: 823230245000

const date2 = new Date('02 Feb 1996 03:04:05 GMT');

console.log(date2.valueOf());

// expected output: 823230245000

getTime() 方法返回一个时间的格林威治时间数值

const moonLanding = new Date('July 20, 69 20:17:40 GMT+00:00');

// milliseconds since Jan 1, 1970, 00:00:00.000 GMT

console.log(moonLanding.getTime());

// expected output: -14182940000

getFullYear() 方法根据本地时间返回指定日期的年份。

const moonLanding = new Date('July 20, 69 00:20:18');

console.log(moonLanding.getFullYear());

// expected output: 1969

getUTCFullYear() 以世界时为标准,返回一个指定的日期对象的年份。

const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');

const date2 = new Date('December 31, 1975, 23:15:30 GMT-11:00');

console.log(date1.getUTCFullYear());

// expected output: 1975

console.log(date2.getUTCFullYear());

// expected output: 1976

getMonth()根据本地时间,返回一个指定的日期对象的月份,为基于 0 的值(0 表示一年中的第一月)。

const moonLanding = new Date('July 20, 69 00:20:18');

console.log(moonLanding.getMonth()); // (January gives 0)

// expected output: 6

getUTCMonth() 方法以世界时为标准,返回一个指定的日期对象的月份,它是从 0 开始计数的(0 代表一年的第一个月)。

const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');

const date2 = new Date('December 31, 1975, 23:15:30 GMT-11:00');

// 十二月

console.log(date1.getUTCMonth());

// expected output: 11

// 一月

console.log(date2.getUTCMonth());

// expected output: 0

getDate()根据本地时间,返回一个指定的日期对象为一个月中的哪一日(从 1--31)

const birthday = new Date('August 19, 1975 23:15:30');

const date1 = birthday.getDate();

console.log(date1);

// expected output: 19

getUTCDate() 方法以世界时为标准,返回一个指定的日期对象为一个月中的第几天。

const date1 = new Date('August 19, 1975 23:15:30 GMT+11:00');

const date2 = new Date('August 19, 1975 23:15:30 GMT-11:00');

console.log(date1.getUTCDate());

// expected output: 19

console.log(date2.getUTCDate());

// expected output: 20

getDay() 方法根据本地时间,返回一个具体日期中一周的第几天,0 表示星期天。对于某个月中的第几天。

const birthday = new Date('August 19, 1975 23:15:30');

const day1 = birthday.getDay();

// Sunday - Saturday : 0 - 6

console.log(day1);

// expected output: 2

getUTCDay() 方法以世界时为标准,返回一个指定的日期对象为一星期中的第几天,其中 0 代表星期天。

const date1 = new Date('August 19, 1975 23:15:30 GMT+11:00');

const date2 = new Date('August 19, 1975 23:15:30 GMT-11:00');

// 星期二

console.log(date1.getUTCDay());

// expected output: 2

// 星期三

console.log(date2.getUTCDay());

// expected output: 3

getHours() 方法根据本地时间,返回一个指定的日期对象的小时。

const birthday = new Date('March 13, 08 04:20');

console.log(birthday.getHours());

// expected output: 4

getUTCHours() 方法以世界时为标准,返回一个指定的日期对象的小时数。

const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');

const date2 = new Date('December 31, 1975, 23:15:30 GMT-11:00');

console.log(date1.getUTCHours());

// expected output: 12

console.log(date2.getUTCHours());

// expected output: 10

getMinutes() 方法根据本地时间,返回一个指定的日期对象的分钟数。

const birthday = new Date('March 13, 08 04:20');

console.log(birthday.getMinutes());

// expected output: 20

getUTCMinutes() 方法以世界时为标准,返回一个指定的日期对象的分钟数。

const date1 = new Date('1 January 2000 03:15:30 GMT+07:00');

const date2 = new Date('1 January 2000 03:15:30 GMT+03:30');

console.log(date1.getUTCMinutes()); // 31 Dec 1999 20:15:30 GMT

// expected output: 15

console.log(date2.getUTCMinutes()); // 31 Dec 1999 23:45:30 GMT

// expected output: 45

getSeconds() 方法根据本地时间,返回一个指定的日期对象的秒数。

const moonLanding = new Date('July 20, 69 00:20:18');

console.log(moonLanding.getSeconds());

// expected output: 18

getUTCSeconds() 方法以世界时为标准,返回一个指定的日期对象的秒数。

const moonLanding = new Date('July 20, 1969, 20:18:04 UTC');

console.log(moonLanding.getUTCSeconds());

// expected output: 4

getMilliseconds() 方法,根据本地时间,返回一个指定的日期对象的毫秒数。

const moonLanding = new Date('July 20, 69 00:20:18');

moonLanding.setMilliseconds(123);

console.log(moonLanding.getMilliseconds());

// expected output: 123

getUTCMilliseconds() 方法以世界时为标准,返回一个指定的日期对象的毫秒数。

const exampleDate = new Date('2018-01-02T03:04:05.678Z'); // 2 January 2018, 03:04:05.678 (UTC)

console.log(exampleDate.getUTCMilliseconds());

// expected output: 678

getTimezoneOffset() 方法返回协调世界时(UTC)相对于当前时区的时间差值,单位为分钟。

const date1 = new Date('August 19, 1975 23:15:30 GMT+07:00');

const date2 = new Date('August 19, 1975 23:15:30 GMT-02:00');

console.log(date1.getTimezoneOffset());

// expected output: 您的本地时区偏移(分钟)

// (eg -120). 不是日期对象的时区偏移量。

console.log(date1.getTimezoneOffset() === date2.getTimezoneOffset());

// expected output: true

setTime() 方法以一个表示从 1970-1-1 00:00:00 UTC 计时的毫秒数为来为 Date 对象设置时间。

const launchDate = new Date('July 1, 1999, 12:00:00');

const futureDate = new Date();

futureDate.setTime(launchDate.getTime());

console.log(futureDate);

// expected output: Thu Jul 01 1999 12:00:00 GMT+0200 (CEST)

const fiveMinutesInMillis = 5 * 60 * 1000;

futureDate.setTime(futureDate.getTime() + fiveMinutesInMillis);

console.log(futureDate);

// expected output: Thu Jul 01 1999 12:05:00 GMT+0200 (CEST)

// (note: your timezone may vary)

setMilliseconds() 方法会根据本地时间设置一个日期对象的豪秒数。

const event = new Date('August 19, 1975 23:15:30');

console.log(event.getMilliseconds());

// expected output: 0

event.setMilliseconds(456);

console.log(event.getMilliseconds());

// expected output: 456

setUTCMilliseconds() 方法会根据世界时来设置指定时间的毫秒数。

const date1 = new Date('2018-01-24T12:38:29.069Z');

console.log(date1.getUTCMilliseconds());

// expected output: 69

date1.setUTCMilliseconds(420);

console.log(date1.getUTCMilliseconds());

// expected output: 420

setSeconds() 方法根据本地时间设置一个日期对象的秒数。

const event = new Date('August 19, 1975 23:15:30');

event.setSeconds(42);

console.log(event.getSeconds());

// expected output: 42

console.log(event);

// Sat Apr 19 1975 23:15:42 GMT+0100 (CET)

// (注意:您的时区可能会有所不同)

setUTCSeconds() 方法为一个依据国际通用时间的特定日期设置秒数。

const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');

console.log(date1.getUTCSeconds());

// expected output: 30

date1.setUTCSeconds(39);

console.log(date1.getUTCSeconds());

// expected output: 39

setMinutes() 方法根据本地时间为一个日期对象设置分钟数。

const event = new Date('August 19, 1975 23:15:30');

event.setMinutes(45);

console.log(event.getMinutes());

// expected output: 45

console.log(event);

// expected output: Tue Aug 19 1975 23:45:30 GMT+0200 (CEST)

// (注意:您的时区可能会有所不同)

setUTCMinutes()方法会根据世界协调时(UTC)来设置指定日期的分钟数。

const date1 = new Date('December 31, 1975, 23:15:30 GMT+11:00');

console.log(date1.getUTCMinutes());

// expected output: 15

date1.setUTCMinutes(25);

console.log(date1.getUTCMinutes());

// expected output: 25

setHours() 方法根据本地时间为一个日期对象设置小时数,返回从 1970-01-01 00:00:00 UTC 到更新后的 日期 对象实例所表示时间的毫秒数。

const event = new Date('August 19, 1975 23:15:30');

event.setHours(20);

console.log(event);

// expected output: Tue Aug 19 1975 20:15:30 GMT+0200 (CEST)

// (注意:您的时区可能会有所不同)

event.setHours(20, 21, 22);

console.log(event);

// expected output: Tue Aug 19 1975 20:21:22 GMT+0200 (CEST)

setUTCHours()方法根据通用时间设置指定日期的小时,并返回自1970年1月1日00:00:00 UTC 以来的毫秒数,直到更新的 Date 实例表示的时间为止。

const event = new Date('August 19, 1975 23:15:30 GMT-3:00');

console.log(event.toUTCString());

// expected output: Wed, 20 Aug 1975 02:15:30 GMT

console.log(event.getUTCHours());

// expected output: 2

event.setUTCHours(23);

console.log(event.toUTCString());

// expected output: Wed, 20 Aug 1975 23:15:30 GMT

setDate() 方法根据本地时间来指定一个日期对象的天数。

const event = new Date('August 19, 1975 23:15:30');

event.setDate(24);

console.log(event.getDate());

// expected output: 24

event.setDate(32);

// Only 31 days in August!

console.log(event.getDate());

// expected output: 1

setUTCDate() 方法就是根据全球时间设置特定 date 对象的日期。

const event = new Date('August 19, 1975 23:15:30 GMT-3:00');

console.log(event.getUTCDate());

// expected output: 20

event.setUTCDate(19);

console.log(event.getUTCDate());

// expected output: 19

setMonth() 方法根据本地时间为一个日期对象设置月份。

const event = new Date('August 19, 1975 23:15:30');

event.setMonth(3);

console.log(event.getMonth());

// expected output: 3

console.log(event);

// Sat Apr 19 1975 23:15:30 GMT+0100 (CET)

// (注意:您的时区可能会有所不同)

setUTCMonth() 方法根据通用的时间来设置一个准确的月份。

const event = new Date('December 31, 1975 23:15:30 GMT-3:00');

console.log(event.toUTCString());

// Thu, 01 Jan 1976 02:15:30 GMT

console.log(event.getUTCMonth());

// expected output: 0

event.setUTCMonth(11);

console.log(event.toUTCString());

// expected output: Wed, 01 Dec 1976 02:15:30 GMT

setFullYear() 方法根据本地时间为一个日期对象设置年份。

const event = new Date('August 19, 1975 23:15:30');

event.setFullYear(1969);

console.log(event.getFullYear());

// expected output: 1969

event.setFullYear(0);

console.log(event.getFullYear());

// expected output: 0

setUTCFullYear() 方法根据世界标准时间为一个具体日期设置年份。

const event = new Date('December 31, 1975 23:15:30 GMT-3:00');

console.log(event.getUTCFullYear());

// expected output: 1976

console.log(event.toUTCString());

// expected output: Thu, 01 Jan 1976 02:15:30 GMT

event.setUTCFullYear(1975);

console.log(event.toUTCString());

// expected output: Wed, 01 Jan 1975 02:15:30 GMT

toUTCString() 方法把一个日期转换为一个字符串,使用 UTC 时区。

const event = new Date('14 Jun 2017 00:00:00 PDT');

console.log(event.toUTCString());

// expected output: Wed, 14 Jun 2017 07:00:00 GMT

toISOString() 方法返回一个 ISO(ISO 8601 Extended Format)格式的字符串: YYYY-MM-DDTHH:mm:ss.sssZ。时区总是 UTC(协调世界时),加一个后缀“Z”标识

const event = new Date('05 October 2011 14:48 UTC');

console.log(event.toString());

// expected output: Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)

// (注意:您的时区可能会有所不同)

console.log(event.toISOString());

// expected output: 2011-10-05T14:48:00.000Z

toJSON() 方法返回 Date 对象的字符串形式。

const event = new Date('August 19, 1975 23:15:30 UTC');

const jsonDate = event.toJSON();

console.log(jsonDate);

// expected output: 1975-08-19T23:15:30.000Z

console.log(new Date(jsonDate).toUTCString());

// expected output: Tue, 19 Aug 1975 23:15:30 GMT

以上方法的具体使用请参考 ES5 标准。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,525评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,203评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,862评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,728评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,743评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,590评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,330评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,244评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,693评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,885评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,001评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,723评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,343评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,919评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,042评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,191评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,955评论 2 355

推荐阅读更多精彩内容