如果我有X个月,使用Javascript,我该如何将该数字转换为X years and X months
?
答案 0 :(得分:5)
你可以使用一个函数来强调单数和复数单词。
function getWords(monthCount) {
function getPlural(number, word) {
return number === 1 && word.one || word.other;
}
var months = { one: 'month', other: 'months' },
years = { one: 'year', other: 'years' },
m = monthCount % 12,
y = Math.floor(monthCount / 12),
result = [];
y && result.push(y + ' ' + getPlural(y, years));
m && result.push(m + ' ' + getPlural(m, months));
return result.join(' and ');
}
var i;
for (i = 0; i < 30; i++) {
console.log(getWords(i));
}
答案 1 :(得分:4)
对于某个数字x
和某个除数y
计算除法(div)
和余数(rem)
为:
var div = Math.floor(x/y);
var rem = x % y;
所以,在你的例子中:
var years = Math.floor(months/12);
var months = months % 12;
答案 2 :(得分:0)
试试这个:
months = 14
console.log(
(months / 12 | 0) + " years and " + months % 12 +" months"
)
答案 3 :(得分:0)
使用此功能
public class MyController : ParentController
{
public MyController()
: this(new UserManager<UserModel>(new UserStore(new MyEntities())))
{
}
public UserManager<UserModel> UserManager { get; private set; }
[HttpPost]
public async Task<JsonResult> SaveUser(UserModel userModel)
{
IdentityResult result = null;
if (userModel.Id > 0) //want to update user with new encrypted password
result = await UserManager.UpdateAsync(user);
else
result = await UserManager.CreateAsync(userModel.EmailId, userModel.Password);
}
}
答案 4 :(得分:0)
要获得年份和月份,您只需要使用此功能:-
<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>
您可以根据需要对此进行改进。这只是一个想法,如何获得解决方案。
答案 5 :(得分:0)
使用此功能,已通过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);
});
});
答案 6 :(得分:0)
var dur1 = Math.floor(months/12);
var dur3 = months - (dur1*12);