如何将Date对象格式化为字符串?
当前回答
Javascript Intl.DateTimeFormat方法提供了一种格式化日期的方便方法。
以下是如何构建所需格式:
常量日期=新日期(“2010-08-10”);let d=新Intl.DateTimeFormat('en-GB',{年:“数字”,月:“短”,日:“2位数”}).format(日期).split(“”).join(“-”);console.log(d);
其他回答
只需执行以下操作:-
let date = new Date().toLocaleDateString('en-us',{day: 'numeric'})
let month = new Date().toLocaleDateString('en-us',{month: 'long'})
let year = new Date().toLocaleDateString('en-us',{year: 'numeric'})
const FormattedDate = `${date}-${month}-${year}`
console.log(FormattedDate) // 26-March-2022
字符串转换
// date
const dateConvert = {
dasher: dt => {
let m = (dt.getMonth() + 1) === 13 ? 1 : (dt.getMonth() + 1);
m = m < 10 ? `0${m}` : m.toString();
let d = dt.getDate();
d = d < 10 ? `0${d}` : d.toString();
return `${dt.getFullYear()}-${m}-${d}`;
},
slasher: dt => {
return dateConvert.slash(dateConvert.dasher(dt));
},
dash: str => {
// 03/11/2022 -> 2022-03-11
let [d, m, y] = str.split('/');
return `${y}-${m}-${d}`;
},
slash: str => {
// 2022-03-11 -> 03/11/2022
let [y, m, d] = str.split('-');
return `${d}/${m}/${y}`
}
}
// console.log(dateConvert.dasher(new Date('01/31/2001')));
嗯,我想要的是将今天的日期转换为MySQL友好的日期字符串,如2012-06-23,并在我的一个查询中使用该字符串作为参数。我找到的简单解决方案是:
var today = new Date().toISOString().slice(0, 10);
请记住,上述解决方案没有考虑您的时区偏移。
您可以考虑改用此函数:
function toJSONLocal (date) {
var local = new Date(date);
local.setMinutes(date.getMinutes() - date.getTimezoneOffset());
return local.toJSON().slice(0, 10);
}
这将为您提供正确的日期,以防您在一天的开始/结束时执行此代码。
var date=新日期();函数到本地(日期){var local=新日期(日期);local.setMinutes(date.getMinutes()-date.getTimezoneOffset());return local.toJSON();}函数到JSONLocal(日期){var local=新日期(日期);local.setMinutes(date.getMinutes()-date.getTimezoneOffset());return local.toJSON().slice(0,10);}//查看您的devtools控制台console.log(date.toJSON());console.log(date.toISOString());console.log(toLocal(日期));console.log(toJSONLocal(日期));
日期.toISOString日期.toJSON字符串切片外部示例
我知道有人可能会说这是一个愚蠢的解决方案,但它确实做到了从日期字符串中删除不必要的信息。
yourDateObject生成:
2017年12月13日星期三20:40:40 GMT+0200(欧洲东部时间)
yourDateObject.toString().sslice(0,15);生产:
2017年12月13日星期三
它在InternetExplorer11、Firefox和Chrome中的工作方式相同(当选择en-UK时,Chrome80.x显示12小时格式)。
const d=新日期('2010/08/05 23:45')//26.3.2020const dtfUK=新Intl.DateTimeFormat('UK',{年:'数字',月:'2位数',日:'2位',小时:'2-位数',分钟:'2-位',秒:'2-数位'})//const dtfUS=新Intl.DateTimeFormat('en',{年:'数字',月:'2位数',日:'2位',小时:'2-位数',分钟:'2-位',秒:'2-数位'})//console.log(dtfUS.format(d));//2010年5月8日下午11:45:00console.log(dtfUK.format(d));//05.08.2010 23:45:00/*node.js:2010年5月8日,晚上11:45:002010-08-05 23:45:00*/
有什么更一般的吗?
var d=新日期('2010-08-10T10:34:56.789Z');var str=d.toDateString()+/-2010年8月10日星期二“”+d.toTimeString().split(“”)[0]+/-12:34:56,GMT+0x0(GMT+00:00)“”+(d.getMonth()+101)+/-108“”+d.获取毫秒();//789console.log(str);//2010年8月10日星期二12:34:56 108 789console.log(//$1周二$2 8月$3 11$4 2020$5 12$6 34$7 56$8 108$9 789str.replace(/(\S{3})(\S}3)(\d{1,2})); // 2010年8月10日12:34.789(星期二)/*$1:星期二工作日字符串$2:8月短文本$3:11天$4:2010年$5:12小时$6:34分钟$7:56秒$8:08个月$9:789毫秒*/
或例如1行IIFE“库”;-)
控制台日志((函数(frm,d){return[d.toDateString(),d.toTimeString().split(“”)[0],(d.getMonth()+101),d.getMilliseconds()].jjoin(“).replace(/(\S{3})(\S})('$4/$8/$3$5:$6($1)',新日期()));
如果不需要,可以删除无用的部件和/或更改索引。
推荐文章
- 如何将两个字符串相加,就好像它们是数字一样?
- 绑定多个事件到一个监听器(没有JQuery)?
- 在JavaScript中将JSON字符串解析为特定对象原型
- 将字符串“true”/“false”转换为布尔值
- 如何使用JavaScript代码获得浏览器宽度?
- event.preventDefault()函数在IE中无法工作
- indexOf()和search()的区别是什么?
- 错误:'types'只能在.ts文件中使用- Visual Studio Code使用@ts-check
- React-Native:应用程序未注册错误
- LoDash:从对象属性数组中获取值数组
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标
- SQL Developer只返回日期,而不是时间。我怎么解决这个问题?
- 如何使用AngularJS获取url参数
- 将RGB转换为白色的RGBA