如何在js中将MM/YYYY格式化为YYYY-MM
How to format MM/YYYY to YYYY-MM in js
我有这个字符串:
var date = "3/2020";
我需要这种格式的日期,如果月份小于 10,则添加 0:
var result = "2020-03";
我已经这样做了:
var date = "3/2020";
var result = date.replace('/', '-');
console.log(result);
我只需要一点帮助来了解如果月份小于 10 时如何添加 0,以及如何更改顺序。有什么建议吗?
请使用 Array.map()、Array.split() 和 Array.reverse() 函数。
像这样。
const date = "3/2020";
const result = date.split('/').map(val => val.length === 1 ? '0' + val : val).reverse().join('-');
console.log(result);
可以用一行来完成(我已经分解成多行来解释它们):
const date = "3/2020";
const dateFormatted = date
.split("/") // [ "3" , "2020" ]
.reverse() // [ "2020" , "3" ]
.map(d => /^\d$/.test(d) ? "0"+ d : d) // transforms "3" into "03" but leaves "2020" intact
.join("-"); // "2020-03"
console.log(dateFormatted)
我建议调查 moment.js
然后你可以创建一个新的日期和设置格式,还可以设置想要的输出格式
const date = moment("3/2020", "MM/YYYY").format("YYYY-MM")
const date2 = moment("11/2020", "MM/YYYY").format("YYYY-MM")
console.log(date)
console.log(date2)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
正则表达式会有所帮助。
const input = "3/2020";
const [_, month, year] = /(\d+)\/(\d*)/.exec(input);
const output =`${year}-${month.toString().padStart(2, "0")}`;
console.log(output);
var date = "3/2020";
dateandmonth = date.split("/");
var event1 = new Date();
event1.setMonth(dateandmonth[0]-1);
event1.setYear(dateandmonth[1]);
MyDateString = (event1.getFullYear() + "-" + ('0' + (event1.getMonth()+1)).slice(-2));
我永远不会说这是一种聪明的做法,但是,为了好玩,您可以仅使用 String.prototype.slice()
in a template string:
来管理所有内容
const input1 = '3/2020';
const output1 = `${(input1).slice(-4)}-${('0' + input1).slice(-7, -5)}`;
//-----------------------------------------^ prepend a '0' in case months < 10
const input2 = '11/2020';
const output2 = `${(input2).slice(-4)}-${('0' + input2).slice(-7, -5)}`;
// test
console.log(output1);
console.log(output2);
显然,这只适用于您确定 input
字符串格式一致且可预测的前提:1 或 2 位数字代表月份,1 个分隔符,4 位数字代表年份。
我不建议在实际产品中使用它,尤其是当输入字符串来自API并且其格式可能会随时间变化时。
您可以使用 date-fns 库。
import { parse, format } from 'date-fns';
const convertDateFormat = (dateStr, oldFormat, newFormat) => {
let date = parse(dateStr, oldFormat, new Date());
return format(date, newFormat);
};
const date = new Date();
const formattedWithSlash = format(date, 'MM/yyyy');
console.log(formattedWithSlash); //'10/2021'
const changedFormat = convertDateFormat(formattedWithSlash, 'MM/yyyy', 'yyyy-MM');
console.log(changedFormat); //'2021-10'
我有这个字符串:
var date = "3/2020";
我需要这种格式的日期,如果月份小于 10,则添加 0:
var result = "2020-03";
我已经这样做了:
var date = "3/2020";
var result = date.replace('/', '-');
console.log(result);
我只需要一点帮助来了解如果月份小于 10 时如何添加 0,以及如何更改顺序。有什么建议吗?
请使用 Array.map()、Array.split() 和 Array.reverse() 函数。
像这样。
const date = "3/2020";
const result = date.split('/').map(val => val.length === 1 ? '0' + val : val).reverse().join('-');
console.log(result);
可以用一行来完成(我已经分解成多行来解释它们):
const date = "3/2020";
const dateFormatted = date
.split("/") // [ "3" , "2020" ]
.reverse() // [ "2020" , "3" ]
.map(d => /^\d$/.test(d) ? "0"+ d : d) // transforms "3" into "03" but leaves "2020" intact
.join("-"); // "2020-03"
console.log(dateFormatted)
我建议调查 moment.js 然后你可以创建一个新的日期和设置格式,还可以设置想要的输出格式
const date = moment("3/2020", "MM/YYYY").format("YYYY-MM")
const date2 = moment("11/2020", "MM/YYYY").format("YYYY-MM")
console.log(date)
console.log(date2)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
正则表达式会有所帮助。
const input = "3/2020";
const [_, month, year] = /(\d+)\/(\d*)/.exec(input);
const output =`${year}-${month.toString().padStart(2, "0")}`;
console.log(output);
var date = "3/2020";
dateandmonth = date.split("/");
var event1 = new Date();
event1.setMonth(dateandmonth[0]-1);
event1.setYear(dateandmonth[1]);
MyDateString = (event1.getFullYear() + "-" + ('0' + (event1.getMonth()+1)).slice(-2));
我永远不会说这是一种聪明的做法,但是,为了好玩,您可以仅使用 String.prototype.slice()
in a template string:
const input1 = '3/2020';
const output1 = `${(input1).slice(-4)}-${('0' + input1).slice(-7, -5)}`;
//-----------------------------------------^ prepend a '0' in case months < 10
const input2 = '11/2020';
const output2 = `${(input2).slice(-4)}-${('0' + input2).slice(-7, -5)}`;
// test
console.log(output1);
console.log(output2);
显然,这只适用于您确定 input
字符串格式一致且可预测的前提:1 或 2 位数字代表月份,1 个分隔符,4 位数字代表年份。
我不建议在实际产品中使用它,尤其是当输入字符串来自API并且其格式可能会随时间变化时。
您可以使用 date-fns 库。
import { parse, format } from 'date-fns';
const convertDateFormat = (dateStr, oldFormat, newFormat) => {
let date = parse(dateStr, oldFormat, new Date());
return format(date, newFormat);
};
const date = new Date();
const formattedWithSlash = format(date, 'MM/yyyy');
console.log(formattedWithSlash); //'10/2021'
const changedFormat = convertDateFormat(formattedWithSlash, 'MM/yyyy', 'yyyy-MM');
console.log(changedFormat); //'2021-10'