JavaScript日期用法介绍和示例

2021年3月17日18:05:15 发表评论 603 次浏览

日期JavaScript中的object用来表示时刻。此时间值为自1970年1月1日UTC(世界标准时间)开始。我们可以使用Date对象创建日期, 方法是调用新的Date()构造函数, 如以下语法所示。

语法如下:

new Date();
new Date(value);
new Date(dateString);
new Date(year, month, day, hours, minutes, seconds, milliseconds);

参数:下面描述了以上语法中显示的所有参数:

  • 值:该值是自1970年1月1日UTC 00:00:00以来的毫秒数。
  • dateString:这表示日期格式。
  • 年 :这用整数值表示, 范围从1900年到1999年。
  • 月份:这由整数值表示, 范围从1月的0到12月的11。
  • 天 :这是一个可选参数。用每月的整数值表示。
  • 小时 :这是可选的。用一天中的小时的整数值表示。
  • 分钟 :这是可选的。用整数值表示一分钟的时间。
  • 秒:这是可选的。这是第二次由整数值表示。
  • 毫秒:这是可选的。用一个毫秒的整数值表示。

返回值:如果未提供任何参数, 则返回当前日期和时间, 否则返回给出参数的日期格式和时间。

让我们看一下Date对象上的JavaScript程序。

范例1:

如果未指定任何参数, 则返回当前日期和时间

<script>
   // If nothing as parameter is given, // it represent the present date and time.
   var A = new Date();
  
   // Printing present date and time.
   document.write(A);
</script>

输出如下:

Wed Mar 21 2018 20:44:40 GMT+0530 (India Standard Time)

范例2:

如果将任何整数值用作参数, 则它给出自1970年1月1日UTC 00:00:00以来的毫秒数。

<script>
   // Parameter as integer value give the number of 
   // milliseconds since January 1, 1970, 00:00:00 UTC.
   var A = new Date(32549);
  
   document.write(A);
</script>

输出如下:

Thu Jan 01 1970 05:30:32 GMT+0530 (India Standard Time)

范例3:

当给定任何dataString作为参数时, 它返回的参数与包含日期名称的参数相同。

<script>
   // When any dataString is given as the parameter 
   // then it return the same as the parameter
   // including day name.
   var A = new Date( 'June 22, 1985 07:19:35' );
  
   document.write(A);
</script>

输出如下:

Sat Jun 22 1985 07:19:35 GMT+0530 (India Standard Time)

范例4:

当一些数字作为参数时, 它们分别被认为是年, 月, 日, 小时, 分钟, 秒, 毫秒。

<script>
   // When some numbers are taken as the parameter 
   // then they are considered as year, month, day, // hours, minutes, seconds, milliseconds 
   // respectively.
   var A = new Date(1996, 10, 13, 5, 30, 22);
  
   document.write(A);
</script>

输出如下:

Wed Nov 13 1996 05:30:22 GMT+0530 (India Standard Time)

错误和异常:要检查这一点, 你必须签入控制台。

范例1:

任何整数都应作为参数, 而不是任何名称, 否则会出错。

<script>
   // Any integer number should be taken 
   // as the parameter not any name.
   var A = new Date(gfg);
  
   document.write(A);
</script>

输出如下:

Error: gfg is not defined

范例2:

任何整数都应作为参数, 而不是其他任何数字, 例如复数。

<script>
   // Any integer number should be take as 
   // the parameter not any other number
   // e.g- complex number.
   var A = new Date(1 + 5i);
  
   document.write(A);
</script>

输出如下:

Error: Invalid or unexpected token

范例3:

任何整数都应作为参数, 而不是其他任何数字, 例如复数。

<script>
   // Any integer number should be taken
   //  as the dateString not word.
   var A = new Date( "lsbin" );
  
   document.write(A);
</script>

输出如下:

Invalid Date

应用:它具有许多应用程序, 例如用于获取确切的当前日期和时间。下面的程序使用Date()对象打印当前日期和时间。

<script>
   // If nothing as parameter is given, it 
   // represent the present date and time.
   var A = new Date();
  
   // Printing present date and time.
   document.write(A);
</script>

输出如下:

Wed Mar 21 2018 20:44:40 GMT+0530 (India Standard Time)

支持的浏览器:支持的浏览器JavaScript日期下面列出:

  • 谷歌浏览器
  • IE浏览器
  • 火狐浏览器
  • 歌剧
  • 苹果浏览器

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: