//取得今年民國年
function getCurrentYear(){
var date = new Date();
return date.getFullYear() - 1911;
}
//取得當下月份
function getCurrentMonth(){
var date = new Date();
return date.getMonth()+1;
}
//取得當下日期
function getCurrentDate(){
var date = new Date();
return date.getDate();
}
//取得當月的第一天(民國年+月+日)
function getFirstDateOfCurrentMonth(){
var date = new Date();
var currentYear = date.getFullYear() - 1911;
var currentMonth = date.getMonth()+1;
return leftPad(String(currentYear), 3) +
leftPad(String(currentMonth), 2)+
leftPad('1', 2);
}
//取得當天日期
function getCurrentDate(){
var date = new Date();
var currentYear = date.getFullYear() - 1911;
var currentMonth = date.getMonth()+1;
var currentDate = date.getDate();
return leftPad(String(currentYear), 3) +
leftPad(String(currentMonth), 2)+
leftPad(currentDate, 2);
}
//取得當月最後一天
function getEndDayInMonth(year, month){
return XDate.getDaysInMonth(year, parseInt(month)-1);
}
//減去指定年,如20141104減去2年,變成20121104
function minusYears(date, year){
return date.addYears(0 - parseInt(year), true).toString('yyyyMMdd');
}
//減去指定月,如20141104減去2個月,變成20140904
function minusMonths(date, month){
return date.addMonths(0 - parseInt(month), true).toString('yyyyMMdd');
}
//確認是否為數字
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
//檢查是串是否不為空值且不為undefined
function isNotEmptyOrUndefined(str){
return str != '' && !angular.isUndefined(str) && str != null;
}
//檢查是否為空值且為undefined
function isEmptyOrUndefined(str){
return str == '' || angular.isUndefined(str) || str == null;
}
//檢查日期起迄,如起始日期為1021101,結束日期為1021103,其會回傳true;
//如起始日期為1021103,結束入其為1021101,其會回傳false
function isValidStartAndEndDate(startDate, endDate){
var isValid = false;
var startYear = parseInt(startDate.substring(0, 3))+1911;
var startMonth = parseInt(startDate.substring(3, 5)-1);
var startDay = parseInt(startDate.substring(5, 7));
var endYear = parseInt(endDate.substring(0, 3))+1911;
var endMonth = parseInt(endDate.substring(3, 5)-1);
var endDay = parseInt(endDate.substring(5, 7));
var sXdate = new XDate(startYear, startMonth, startDay);
var eXdate = new XDate(endYear, endMonth, endDay);
var diffDays = sXdate.diffDays(eXdate);
if(diffDays < 0){
isValid = false;
}else{
isValid = true;
}
return isValid;
}
//檢查日期起迄,如起始日期為1020101,結束日期為1020131,其會回傳true;
//如起始日期為1031103,結束入其為1021101,其會回傳false
function isValidStartAndEndYYYMM(startDate, endDate){
var isValid = false;
var startYear = parseInt(startDate.substring(0, 3))+1911;
var startMonth = parseInt(startDate.substring(3, 5)-1);
var startDay = parseInt(startDate.substring(5, 7));
var endYear = parseInt(endDate.substring(0, 3))+1911;
var endMonth = parseInt(endDate.substring(3, 5)-1);
var endDay = parseInt(endDate.substring(5, 7));
var sXdate = new XDate(startYear, startMonth, startDay);
var eXdate = new XDate(endYear, endMonth, endDay);
var diffDays = sXdate.diffMonths(eXdate);
if(diffDays < 0){
isValid = false;
}else{
isValid = true;
}
return isValid;
}
//西元轉民國,如20141104轉1021104
function convertFromWesternToChinse(western){
var year = parseInt(western.substring(0, 4))-1911;
var monthDate = western.substring(4, western.length);
return leftPad(String(year), 3)+monthDate;
}
//若未滿位,左邊補0
function leftPad(val, length) {
var str = '' + val;
while (str.length < length) {
str = '0' + str;
}
return str;
}
註: 有用到xdate字眼的部分,要到 http://arshaw.com/xdate/ 這裡下載js檔