JavaScript Date对象

和日期函数杠上了,后面还要杠一杠Math方法:

JavaScript Date对象,Date对象:此对象用于处理日期和时间。

创建Date对象:new Date(),既然创建了该对象,我们就要想办法用到它,

创建Date对象的四种方法

var d = new Date();
var d = new Date(milliseconds);
var d = new Date(dateString);
var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);

1、获取当日的日期,  let date = new Date();   document.write(date); 

打印结果:Thu Sep 12 2019 14:21:06 GMT+0800 (中国标准时间)

是不是很怪异,好像哪里不对,这我们部分中国人不认识它啊,来给他理理发。

等等,我好像错过了什么,我的format函数去哪了?(new Date()).format("yyyy年MM月dd日"),可恶的js竟然不认识format函数。还好的是js提供了,toLocaleDateString方法,

是的!就是我们的JavaScript toLocaleDateString()方法根据本地时间将Date对象转换为字符串,可谓是神不知鬼不觉,看案例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="test">
        当前日期
    </div>
</body>
    <script>
        var myDate = new Date();
       document.getElementById("test").innerHTML = myDate.toLocaleDateString()
       console.log(myDate.toLocaleDateString());
    </script>
</html>

打印结果:2019/9/12

好像还是咋对劲,我们想要的结果万一是2019年9月12日不是很尴尬,还好,我们有split()方法,来吧!宝贝跟上节奏,music

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="test">
        当前日期
    </div>
</body>
    <script>
        var myDate = new Date();
       document.getElementById("test").innerHTML = myDate.toLocaleDateString()
       console.log(myDate.toLocaleDateString());
       var test = myDate.toLocaleDateString();
       var testOne = test.split('/')
        document.write("然后就嘿嘿嘿");
       var success = '<br/>' + testOne[0] + '年' +testOne[1] + '月' +testOne[2]+ '日' +'<br/>' + testOne[0] + '-' +testOne[1] + '-' +testOne[2];
       document.write(success);
       document.write('<br/>'+"什么你不喜欢这种字符串连接方式,好我们换一种");
       var testTwo = testOne.join('-');
       document.write('<br/>'+testTwo);
       document.write('<br/>'+"具体用法随你而定");
    </script>
</html>

什么?你想玩时分秒!js提供的有

getFullYear():从Date对象以四位数返回年份。

getMonth(): 从Date对象返回月份(0-11)。

getDate(): 从Date对象返回一个月中的某一天(1-31);

getDay(): 从Date对象返回一周中的某一天(0-6)。

getHours():返回Date对象的小时(0-23)。

getMinutes():返回Date对象的分钟(0-59) 。

getSeconds():  返回Date对象的秒数(0-59)。

get Milliseconds():  返回Date对象的毫秒(0-999)。

getTime(): 返回1970年1月1日至今的毫秒数。

综上方法已经足以应付大多数时间问题了

我们来探讨下下一个问题,如果我想返回的是前一周的某天具体日期该怎么办?

 var myDateOne = new Date((new Date()).getTime() - 7*24*60*60*1000).toLocaleDateString();

 document.write('<br/>'+myDateOne);

打印结果:2019/9/5

剩下的就没啥了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值