JavaScript 日期对象互转String对象,葫芦娃葫芦娃~

日期转字符串:

date = new Date()
//第一种是直接toString() 
date.toString()

但是格式是这样的:
VM153:3 Thu Sep 28 2017 09:26:05 GMT+0800 (中国标准时间)
有卵用啊这种格式

所以得用第二种:

通过获取年月日来拼接字符串

let date = new Date()

let date_str = date.getFullYear() + "-" +
               date.getMonth() + "-" +
               date.getDate()

String转Date:

let date_str = "2019-11-11"

let date = new Date(Date.parse(date_str.replace(/-/g, "/")));

// 输出格式就是这样的 不过是Date类型
VM153:3 Thu Sep 28 2017 09:26:05 GMT+0800 (中国标准时间)
发布了239 篇原创文章 · 获赞 31 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/soulwyb/article/details/100009615