Date
日期对象
基本使用
var now = new Date(); // Mon Mar 28 2022 13:48:27 GMT+0800 (中国标准时间)
// 年
now.getFullYear(); // 2022
// 月 0~11 代表月
now.getMonth(); // 2 (其实现在是3月)
// 日
now.getDate(); // 28
// 周几
now.getDay(); // 1
// 时
now.getHours(); // 13
// 分
now.getMinutes(); // 51
// 秒
now.getSeconds(); // 58
// 时间戳,全世界统一,1970-01-01 00:00:00 到现在的毫秒数
now.getTime(); // 1648446718640
// 时间戳转为时间
console.log(new Date(1648446718640));
// Mon Mar 28 2022 13:51:58 GMT+0800 (中国标准时间)
转换
var now = new Date();
now.toDateString(); // 'Mon Mar 28 2022'
now.toGMTString(); // 'Mon, 28 Mar 2022 05:51:58 GMT'
now.toISOString(); // '2022-03-28T05:51:58.640Z'
now.toLocaleDateString(); // '2022/3/28'
now.toLocaleString(); // '2022/3/28 13:51:58'
JSON
- JSON(JavaScript Object Notation,JS 对象简谱)是一种轻量级得数据交换格式。
- 简洁和清晰得层次结构使得 JSON 成为理想得数据交换语言。
- 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
在 JavaScript 中一切皆为对象,任何 js 支持的类型都可以用 JSON 来表示;
格式:
- 对象都用 {}
- 数组都用 []
- 所有的键值对都是用 key: value
JSON 和 JS 对象转换
var person = {
name: 'jack',
age: 20,
sex: '男'
}
// 对象转化为 JSON 字符串
var jsonPerson = JSON.stringify(person);
// JSON 字符串转换为对象
var objectPerson = JSON.parse(jsonPerson);