我想从循环中获取实际的月份。当我在开发人员工具中执行此操作并且month[i]
== 4时,它不会将actualMonth
分配给checkMonth
我是否必须将getMonth
分配给month[]
,然后尝试查询该值?
var showCurrentMonth = function() {
var getMonth = new Date().getMonth();
var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var actualMonth = "";
for (var i = 0; i < month.length; i++) {
var checkMonth = month[i];
console.log(month[i]);
if (getMonth == month[i]) {
actualMonth = checkMonth;
}
}
console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);
答案 0 :(得分:2)
您的问题是您正在比较整数和字符串:
var getMonth = new Date().getMonth(); // This return number from 0 to 11
代码Date().getMonth()
返回一个整数,您的month
列表中包含字符串
您的代码应为:
var showCurrentMonth = function() {
var getMonth = new Date().getMonth();
var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var actualMonth = month[getMonth];
console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);
要获得实际月份,您只需要使用 getMonth 作为索引访问月份列表
答案 1 :(得分:2)
太简单了?
var showCurrentMonth = function() {
var getMonth = new Date().getMonth();
var month = ["january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"];
var actualMonth = month[getMonth];
console.log(actualMonth);
}
window.addEventListener('DOMContentLoaded', showCurrentMonth, false);
答案 2 :(得分:2)
将您的if更改为.a
这样做:
if (month[getMonth] == month[i]) {
答案 3 :(得分:1)
slightly Modification required
package com.projectShaun.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.projectShaun.dao.AccountDao;
import com.projectShaun.model.Account;
@Service("accountService")
@Transactional
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
public void persistAccount(Account account) {
accountDao.persistAccount(account);
}
}