首页 技术 正文
技术 2022年11月20日
0 收藏 825 点赞 4,749 浏览 739 个字

js的Date对象不怎么经常用,所以忘得差不多,复习一下

1.声明一个Date对象,默认本地当前时间

var date = new Date();//Fri Apr 28 2017 14:26:19 GMT+0800 (中国标准时间)

 如果想声明一个具体的时间,可以有以下几种方式:

new Date("yyyy/mth/dd");
new Date("month dd,yyyy hh:mm:ss");
new Date("month dd,yyyy");
new Date(yyyy,mth,dd,hh,mm,ss);
new Date(yyyy,mth,dd);
new Date(ms);//参数表示需要创建的时间和GMT时间1970年1月1日之间相差的毫秒数

需要注意的是月的取值范围为0~11

2.Date对象的方法

常用的Date对象的方法分set和get两种方式。记录几个容易忘记或者混淆的方法

getDate()    //从 Date 对象返回一个月中的某一天 (1 ~ 31)。
getDay() //从 Date 对象返回一周中的某一天 (0 ~ 6)。
getMonth() //从 Date 对象返回月份 (0 ~ 11)。
getTime() //返回 1970 年 1 月 1 日至今的毫秒数。
parse("xxx") //返回1970年1月1日午夜到指定日期(字符串)的毫秒数。
toString() //把 Date 对象转换为字符串。
toTimeString() //把 Date 对象的时间部分转换为字符串。
toDateString() //把 Date 对象的日期部分转换为字符串。
Date.now()

3.关于格式的一些场景

在Angularjs中,可是使用$filter

var myJsDate=$filter('date')($scope.myDate,'yyyy-MM-dd');
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,580
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918