Javascript 如何将月数转换为月数和年数?
如果我有X个月,使用Javascript,我如何将该数字转换为X年和X个月?对于某些数字X和某些除数y,计算除法div和余数rem为: 因此,在您的示例中:Javascript 如何将月数转换为月数和年数?,javascript,Javascript,如果我有X个月,使用Javascript,我如何将该数字转换为X年和X个月?对于某些数字X和某些除数y,计算除法div和余数rem为: 因此,在您的示例中: var years = Math.floor(months/12); var months = months % 12; 试试这个: 月=14 console.log 月/12 | 0+年和+月%12+月 你可以使用一个强调单数和复数的函数 函数getWordsmonthCount{ 函数getPluralnumber,word{ 返
var years = Math.floor(months/12);
var months = months % 12;
试试这个:
月=14
console.log
月/12 | 0+年和+月%12+月
你可以使用一个强调单数和复数的函数 函数getWordsmonthCount{ 函数getPluralnumber,word{ 返回号码===1&&word.one | | word.other; } var months={1:'month',其他:'months'}, 年份={1:'年',其他:'年'}, m=月数%12, y=Math.floormonthCount/12, 结果=[]; y&&result.pushy+“”+getPluraly,年; m&&result.pushm+''+getPluralm,月; 返回结果。加入'and'; } var i; 对于i=0;i<30;i++{ console.loggetWordsi; } 使用此功能
function months2years(months) {
var dur1 = Math.floor(months/12)
var dur2 = (months/12)-dur1
var dur3 = Math.floor(dur2*12)
return dur1+" years and "+dur3+" months"
}
要获得年份和月份,您只需使用此功能:-
<script type="text/javascript">
function years_months(month)
{
if(month==12) {
var y1 = 1 + " Year";
return y1;
} else {
var y=(month/12);
var mm=(month%12);
var ym = y + "Years"+" " + mm + "Months";
return ym;
}
}
</script>
您可以根据需要对此进行改进。这只是一个想法,您可以如何获得解决方案。使用此函数,它通过Jest单元测试进行测试
function durationFormatter(monthsCount) {
const years = Math.floor(monthsCount / 12);
let yearsDur = years > 0 ? `${years} year` : '';
yearsDur = years > 1 ? `${yearsDur}s` : yearsDur;
const restMonths = monthsCount - years * 12;
let monthsDur = restMonths > 0 ? `${restMonths} month` : '';
monthsDur = restMonths > 1 ? `${monthsDur}s` : monthsDur;
const and = years > 0 && restMonths > 0 ? ', and ' : '';
return `${yearsDur}${and}${monthsDur}`;
}
测试套件
describe('Duration formatter', () => {
test('formate 12 months duration to 1 year', () => {
const formatedDuration = durationFormatter(12);
const expected = '1 year';
expect(formatedDuration).toBe(expected);
});
test('formate 24 months duration to 2 years', () => {
const formatedDuration = durationFormatter(24);
const expected = '2 years';
expect(formatedDuration).toBe(expected);
});
test('formate 6 months duration to 6 months', () => {
const formatedDuration = durationFormatter(6);
const expected = '6 months';
expect(formatedDuration).toBe(expected);
});
test('formate 1 month duration to 1 month', () => {
const formatedDuration = durationFormatter(1);
const expected = '1 month';
expect(formatedDuration).toBe(expected);
});
test('formate 13 months duration to 1 year and 1 month', () => {
const formatedDuration = durationFormatter(13);
const expected = '1 year, and 1 month';
expect(formatedDuration).toBe(expected);
});
test('formate 14 months duration to 1 year and 2 months', () => {
const formatedDuration = durationFormatter(14);
const expected = '1 year, and 2 months';
expect(formatedDuration).toBe(expected);
});
test('formate 25 months duration to 2 years and 1 month', () => {
const formatedDuration = durationFormatter(25);
const expected = '2 years, and 1 month';
expect(formatedDuration).toBe(expected);
});
test('formate 26 months duration to 2 years and 2 months', () => {
const formatedDuration = durationFormatter(26);
const expected = '2 years, and 2 months';
expect(formatedDuration).toBe(expected);
});
});
风险值x=14,月=x%12,年=x-月/12;-我会让你做的字符串工作干净,简单,做的工作完美。谢谢虽然这个代码片段可能是解决方案,但它确实有助于提高文章的质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。
describe('Duration formatter', () => {
test('formate 12 months duration to 1 year', () => {
const formatedDuration = durationFormatter(12);
const expected = '1 year';
expect(formatedDuration).toBe(expected);
});
test('formate 24 months duration to 2 years', () => {
const formatedDuration = durationFormatter(24);
const expected = '2 years';
expect(formatedDuration).toBe(expected);
});
test('formate 6 months duration to 6 months', () => {
const formatedDuration = durationFormatter(6);
const expected = '6 months';
expect(formatedDuration).toBe(expected);
});
test('formate 1 month duration to 1 month', () => {
const formatedDuration = durationFormatter(1);
const expected = '1 month';
expect(formatedDuration).toBe(expected);
});
test('formate 13 months duration to 1 year and 1 month', () => {
const formatedDuration = durationFormatter(13);
const expected = '1 year, and 1 month';
expect(formatedDuration).toBe(expected);
});
test('formate 14 months duration to 1 year and 2 months', () => {
const formatedDuration = durationFormatter(14);
const expected = '1 year, and 2 months';
expect(formatedDuration).toBe(expected);
});
test('formate 25 months duration to 2 years and 1 month', () => {
const formatedDuration = durationFormatter(25);
const expected = '2 years, and 1 month';
expect(formatedDuration).toBe(expected);
});
test('formate 26 months duration to 2 years and 2 months', () => {
const formatedDuration = durationFormatter(26);
const expected = '2 years, and 2 months';
expect(formatedDuration).toBe(expected);
});
});
var dur1 = Math.floor(months/12);
var dur3 = months - (dur1*12);