如何在 javascript 中获取 formate dd/mm/yyyy 中的年份
How to get year in formate dd/mm/yyyy in javascript
例如
var date = '18-02-2015';
我怎样才能得到年份?只是那一年。
如果我使用 getFullYear() -> 它只处理甲酸盐 MM-DD-YYYY
抱歉我的英语不好
希望对您有所帮助。
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
var today = dd+'/'+mm+'/'+yyyy;
如果您知道格式始终是 dd-mm-yyyy,您可以这样做
var date = "18-02-2015"
var year = date.substring(date.lastIndexOf("-")+1)
如果您确定格式为 dd-MM-YYYY,则拆分字符串并切换它,然后将其解析为日期。
var date = '18-02-2015';
var input = date.split("-");
var dateObject = new Date(input[2] +"-"+ input[1] +"-"+ input[0]);
console.log(dateObject);
var year = dateObject.getFullYear();
//or without parsing simply
var year = input[2];
你看过了吗http://momentjs.com/
这应该可以帮助您获得年份。
var day = moment("18-02-2015", "MM-DD-YYYY");
console.log(day.format("YYYY"));
使用正则表达式:
var year = function(str) {
return str.match(/\d{4}$/)[0]; // \d{4}, four digits group from the end
};
alert(year('18/02/2015'));
alert(year('02/18/2004'));
使用 slice():
var year = function(str) {
return str.slice(-4);// get last four characters
};
alert(year('01/12/2015'));
alert(year('18/02/2004'));
如果您使用的日期只是一个字符串,那么您要做的就是获取该字符串的最后 4 个字符,它们构成了年份。
假设日期始终采用您列出的格式,请使用 slice():
var date = '18-02-2015';
var year = date.slice(-4);
如果日期需要是整数类型,那么使用这个代替:
var year = parseInt(date.slice(-4));
一种利用 HTML 的 <input type="date" />
元素的方法是:
function findYearFrom() {
// if it's not of 'type="date"', or it has no value,
// or the value is equal to the default-value:
if (this.type !== 'date' || !this.value || this.value === this.defaultValue) {
// we return here
return false;
}
// we get the value of the element as a date, and then
// call getFullYear() on that value:
console.log(this.valueAsDate.getFullYear());
return this.valueAsDate.getFullYear();
}
// binding the named-function as the change event-handler
// for this element:
document.getElementById('demo').addEventListener('change', findYearFrom);
<input id="demo" type="date" value="2015-02-18" />
例如
var date = '18-02-2015';
我怎样才能得到年份?只是那一年。 如果我使用 getFullYear() -> 它只处理甲酸盐 MM-DD-YYYY
抱歉我的英语不好
希望对您有所帮助。
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
var today = dd+'/'+mm+'/'+yyyy;
如果您知道格式始终是 dd-mm-yyyy,您可以这样做
var date = "18-02-2015"
var year = date.substring(date.lastIndexOf("-")+1)
如果您确定格式为 dd-MM-YYYY,则拆分字符串并切换它,然后将其解析为日期。
var date = '18-02-2015';
var input = date.split("-");
var dateObject = new Date(input[2] +"-"+ input[1] +"-"+ input[0]);
console.log(dateObject);
var year = dateObject.getFullYear();
//or without parsing simply
var year = input[2];
你看过了吗http://momentjs.com/
这应该可以帮助您获得年份。
var day = moment("18-02-2015", "MM-DD-YYYY");
console.log(day.format("YYYY"));
使用正则表达式:
var year = function(str) {
return str.match(/\d{4}$/)[0]; // \d{4}, four digits group from the end
};
alert(year('18/02/2015'));
alert(year('02/18/2004'));
使用 slice():
var year = function(str) {
return str.slice(-4);// get last four characters
};
alert(year('01/12/2015'));
alert(year('18/02/2004'));
如果您使用的日期只是一个字符串,那么您要做的就是获取该字符串的最后 4 个字符,它们构成了年份。
假设日期始终采用您列出的格式,请使用 slice():
var date = '18-02-2015';
var year = date.slice(-4);
如果日期需要是整数类型,那么使用这个代替:
var year = parseInt(date.slice(-4));
一种利用 HTML 的 <input type="date" />
元素的方法是:
function findYearFrom() {
// if it's not of 'type="date"', or it has no value,
// or the value is equal to the default-value:
if (this.type !== 'date' || !this.value || this.value === this.defaultValue) {
// we return here
return false;
}
// we get the value of the element as a date, and then
// call getFullYear() on that value:
console.log(this.valueAsDate.getFullYear());
return this.valueAsDate.getFullYear();
}
// binding the named-function as the change event-handler
// for this element:
document.getElementById('demo').addEventListener('change', findYearFrom);
<input id="demo" type="date" value="2015-02-18" />