JS 取当前日期、时间的代码 收藏
1.JS 取当前日期、时间的代码?
--------------------------------------------------------------------------------
view plaincopy to clipboardprint?
2.如何用js得到 yyyy-mm-dd格式的当前日期?
--------------------------------------------------------------------------------
view plaincopy to clipboardprint?
function appendZero(s){return ("00"+ s).substr((s+"").length);} //补0函数
var d = new Date();
alert(d.getFullYear() + "-" + appendZero(d.getMonth() + 1) + "-" + appendZero(d.getDate()));
function appendZero(s){return ("00"+ s).substr((s+"").length);} //补0函数
var d = new Date();
alert(d.getFullYear() + "-" + appendZero(d.getMonth() + 1) + "-" + appendZero(d.getDate()));
3.javascript 获得指定日期的临近日期的方法
view plaincopy to clipboardprint?
//取得日期字符串,返回YYYY-MM-DD
function getDate(date)
{
var thisYear = date.getYear();
var thisMonth = date.getMonth() + 1;
//如果月份长度是一位则前面补0
if(thisMonth<10) thisMonth = "0" + thisMonth;
var thisDay = date.getDate();
//如果天的长度是一位则前面补0
if(thisDay<10) thisDay = "0" + thisDay;
return thisYear + "-" + thisMonth + "-" + thisDay;
}
//取得日期时间字符串,返回YYYY-MM-DD HH:mm:SS
function getDateTime(date)
{
var thisYear = date.getYear();
var thisMonth = date.getMonth() + 1;
//如果月份长度是一位则前面补0
if(thisMonth<10) thisMonth = "0" + thisMonth;
var thisDay = date.getDate();
//如果天的长度是一位则前面补0
if(thisDay<10) thisDay = "0" + thisDay;
var thisHour = date.getHours();
//如果小时长度是一位则前面补0
if(thisHour<10) thisHour = "0" + thisHour;
var thisMinute = date.getMinutes();
//如果分钟长度是一位则前面补0
if(thisMinute<10) thisMinute = "0" + thisMinute;
var thisSecond = date.getSeconds();
//如果分钟长度是一位则前面补0
if(thisSecond<10) thisSecond = "0" + thisSecond;
return thisYear + "-" + thisMonth + "-" + thisDay + " " + thisHour + ":" + thisMinute + ":" + thisSecond;
}
//根据日期字符串生成日期对象,日期字符串格式为YYYY-MM-DD
function setDate(strDate)
{
var aDate = strDate.split("-");
return new Date(aDate[0],aDate[1]-1,aDate[2]);
}
//获得指定日期的临近日期
//strDate:指定的日期,格式为yyyy-mm-dd nDay:与指定日期相邻的天数 1为明天 -1为昨天
function getNearDay(strDate,nDay)
{
try
{
var oDate = setDate(strDate);
var newDate = new Date(oDate.valueOf() + nDay*24*60*60*1000);
return getDate(newDate);
}
catch(ex)
{
return "error";
}
}