1.获取本地时间
dateTime: Date;
this.dataTime = new Date(); // 获取当前日期
// 输出: Fri Dec 08 2017 00:33:01 GMT+0800 (中国标准时间)
查看Date具体有哪些类型的输出
console.log('orderdate:', this.OrderDate);
console.log('toISOString:', this.dataTime.toISOString());
console.log('toDateString:', this.dataTime.toDateString());
console.log('toUTCString:', this.dataTime.toUTCString());
console.log('toJSON:', this.dataTime.toJSON());
console.log('toLocaleDateString:', this.dataTime.toLocaleDateString());
console.log('toLocaleString:', this.dataTime.toLocaleString());
console.log('toLocaleTimeString:', this.dataTime.toLocaleTimeString());
console.log('toString:', this.dataTime.toString());
console.log('toTimeString:', this.dataTime.toTimeString());
// 输出结果:
// toISOString: 2017-12-07T16:33:01.843Z
// toDateString: Fri Dec 08 2017
// toUTCString: Thu, 07 Dec 2017 16:33:01 GMT
// toJSON: 2017-12-07T16:33:01.843Z
// toLocaleDateString: 2017/12/8
// toLocaleString: 2017/12/8 上午12:33:01
// toLocaleTimeString: 上午12:33:01
// toString: Fri Dec 08 2017 00:33:01 GMT+0800 (中国标准时间)
// toTimeString: 00:33:01 GMT+0800 (中国标准时间)
2.很多时候数据库中希望存的样式是: ‘yyyy-MM-dd HH:mm:ss’, 但是上面的输出并没有
注意: toISOString() 方法和 toJSON() 方法输出虽然样式相同, 但是时间时区不同
前台html页面日期格式化:
{{ dateTime | date:'yyyy-MM-dd HH:mm:ss'}}
{{ dateTime | date:'medium' }}
{{ dateTime | date:'short' }}
{{ dateTime | date:'fullDate' }}
{{ dateTime | date:'longDate' }}
{{ dateTime | date:'mediumDate' }}
{{ dateTime | date:'shortDate' }}
{{ dateTime | date:'mediumTime' }}
{{ dateTime | date:'shortTime' }}
网友评论