《DateUtil.js》多种日期格式直接调用即可 方法名可优化

56 篇文章 0 订阅
36 篇文章 1 订阅

抽时间做笔记:在js中每次都要拼接时间,为了方便,整理了一个公用的DateUtil.js。直接调用即可。其中方法名称可以进一步优化哦。

先看下时间格式,如下:

测试页面:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>js基础-时间处理</title>
    <script src="DateUtil.js"></script>
</head>
<body>
<script type="text/javascript">
    console.log(date);//Wed Jul 20 2007 09:31:52 GMT+0800 (中国标准时间)
    console.log(date.toDateString());//Wed Jul 20 2007
    console.log(date.toTimeString());//09:42:17 GMT+0800 (中国标准时间)
    console.log(date.toLocaleDateString());//2007/7/20
    console.log(date.toLocaleTimeString());//上午9:42:17
    console.log(date.toUTCString());//Wed, 20 Jul 2007 01:42:17 GMT
    console.log(date.getFullYear());//年
    console.log(getdate_yy());//年后两位数
    console.log(getdate_Month())//月
    console.log(date.getDate())//日
    console.log(+date.getHours())//时
    console.log(date.getMinutes())//分
    console.log(date.getSeconds())//秒
    console.log(getdate_stamp());//输出时间戳(毫秒数)
    console.log(getdate_yMd());
    console.log(getdate_yMdW());
    console.log(getdate_yMdhm());
    console.log(getdate_yMdhms());
    console.log(getdate_yMdhmsW());
    console.log(getdate_yMd_T());
    console.log(getdate_yMdW_T());
    console.log(getdate_yMd_C());
    console.log(getdate_yMdW_C());
    console.log(getdate_yMdhms_T());
    console.log(getdate_yMdhmsW_T());
    console.log(getdate_WyMdhms_T());
    console.log(getdate_yMdhms_C());
    console.log(getdate_yMdWhms_C());
    console.log(getdate_yMdhmsW_C());
    console.log(getdate_WyMdhms_C());
    console.log(getdate_Hms());
    console.log(getdate_Hms_C());
    console.log(getdate_Md_C());
    console.log(getdate_MdW_C());
    console.log(getdate_Md());
    console.log(getdate_MdW());
    console.log(weekCh);
    console.log(weekEn);
    console.log(weekEns);
    console.log(getdate_Mdys_En());
    console.log(getdate_Mdys_Ens());
    console.log(getdate_Mdy_En());
    console.log(getdate_dMys_En());
    console.log(getdate_dMy_En());
    console.log(getdate_WMdys_En());
    console.log(getdate_WMdy_En());
    console.log(tdoa('2020-04-12 11:02:14','2020-09-11 23:12:34'));//tdoa(datestart,dateend) 信息输出:剩余: 152天12小时10分20秒


</script>
</body>
</html>

 DateUtil.js

var date = new Date();
var datestr;
var yMd=date.getFullYear() + "年" + (date.getMonth() + 1) + "月" + date.getDate() + "日";
var hms=date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
var hms_ch= date.getHours() + "时" + date.getMinutes() + "分" + date.getSeconds() + "秒";
var yMd_n=date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate();
var yMds=date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
var datestrinfo=yMds + " " + hms;
var datestrinfo2=yMd+" "+hms_ch;
var datestrinfo3 = yMd_n+ " " + hms;

/*星期*/
var weekindex = date.getDay();
var weeks = ['日', '一', '二', '三', '四', '五', '六'];
var weekEs = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var weekE = ['Sun.', 'Mon.', 'Tues.', 'Wed.', 'Thurs.', 'Fri.', 'Sat.'];
var weekCh = "星期" + weeks[weekindex];
var weekEn = weekE[weekindex];
var weekEns = weekEs[weekindex];
/*月份*/
var montharrs=['January','February','March','April','May','June','July','August','September','October','November','December'];
var montharr=['Jan.','Feb.','Mar.','Apr.','May.','Jun.','Jul.','Aug.','Sept.','Oct.','Nov.','Dec.'];
var month=montharr[date.getMonth()];
var months=montharrs[date.getMonth()];
/*时间戳-毫秒数*/
function getdate_stamp() {
    return date.getTime();
}
/*算时间差*/
function tdoa(datestart,dateend) {
    var daynum=86400;//一天秒数(24*60*60)
    var nums=3600;//60*60
    var tend=new Date(dateend).getTime();
    var tstart=new Date(datestart).getTime();
    var difference = (tend-tstart)/1000;
    var day = parseInt(difference / daynum);//天数
    var Seconds = difference - day*daynum;//剩余秒数
    var hour = parseInt(Seconds/nums);//整数小时数
    var seHour = difference - day*daynum - hour*nums;//小时数后剩余秒数
    var Minutes = parseInt(seHour/60);//整数分
    var tdMin = difference - day*daynum - hour*nums - Minutes*60;//分后剩余秒数
    var infotdoa="剩余: "+day+"天"+hour+"小时"+Minutes+"分"+tdMin+"秒";
    return infotdoa;
}
/*MM*/
function getdate_Month() {
    return date.getMonth() + 1;
}

/*yy*/
function getdate_yy() {
    var year=date.getFullYear().toString().substr(2,3);
    return year;
}

/*yyyy-MM-dd*/
function getdate_yMd_T() {
    datestr = yMds;
    return datestr;
}
/*yyyy-MM-dd*/
function getdate_yMdW_T() {
    datestr = yMds+" "+weekCh;
    return datestr;
}
/*yyyy/MM/dd */
function getdate_yMd() {
    datestr = yMd_n;
    return datestr;
}

/*yyyy/MM/dd */
function getdate_yMdW() {
    datestr = yMd_n+ " " + weekCh;
    return datestr;
}

/*yyyy/MM/dd HH:mm */
function getdate_yMdhm() {
    datestr = yMd_n + " " + date.getHours() + ":" + date.getMinutes();
    return datestr;
}

/*yyyy/MM/dd HH:mm:ss */
function getdate_yMdhms() {
    datestr = datestrinfo3;
    return datestr;
}
/*yyyy/MM/dd HH:mm:ss 星期*/
function getdate_yMdhmsW() {
    datestr = datestrinfo3+" "+weekCh;
    return datestr;
}
/*yyyy-MM-dd HH:mm:ss */
function getdate_yMdhms_T() {
    datestr = datestrinfo;
    return datestr;
}
/*yyyy-MM-dd HH:mm:ss 星期*/
function getdate_yMdhmsW_T() {
    datestr = datestrinfo+" "+weekCh;
    return datestr;
}
/* 星期 yyyy-MM-dd HH:mm:ss */
function getdate_WyMdhms_T() {
    datestr =weekCh+" "+ datestrinfo;
    return datestr;
}

/*yyyy年MM月dd日*/
function getdate_yMd_C() {
    datestr = yMd;
    return datestr;
}

/*yyyy年MM月dd日 星期*/
function getdate_yMdW_C() {
    datestr = yMd+" "+ weekCh;
    return datestr;
}

/*MM-dd*/
function getdate_Md() {
    datestr = (date.getMonth() + 1) + "-" + date.getDate();
    return datestr;
}
/*MM-dd 星期*/
function getdate_MdW() {
    datestr = (date.getMonth() + 1) + "-" + date.getDate()+" "+weekCh;
    return datestr;
}

/*MM月dd日*/
function getdate_Md_C() {
    datestr = (date.getMonth() + 1) + "月" + date.getDate() + "日";
    return datestr;
}

/*MM月dd日 星期*/
function getdate_MdW_C() {
    datestr = (date.getMonth() + 1) + "月" + date.getDate() + "日"+" "+weekCh;
    return datestr;
}
/*yyyy年MM月dd日 HH时mm分ss秒 */
function getdate_yMdhms_C() {
    datestr = datestrinfo2;
    return datestr;
}

/*yyyy年MM月dd日 星期 HH时mm分ss秒 */
function getdate_yMdWhms_C() {
    datestr =yMd +" "+weekCh+" "+hms_ch;
    return datestr;
}

/*yyyy年MM月dd日 HH时mm分ss秒 星期 */
function getdate_yMdhmsW_C() {
    datestr = datestrinfo2 +" "+ weekCh;
    return datestr;
}
/*星期 yyyy年MM月dd日 HH时mm分ss秒  */
function getdate_WyMdhms_C() {
    datestr = weekCh+" "+datestrinfo2;
    return datestr;
}
/*HH:mm:ss */
function getdate_Hms() {
    datestr = hms;
    return datestr;
}

/*HH时mm分ss秒 */
function getdate_Hms_C() {
    datestr = hms_ch;
    return datestr;
}

/*Month day year*/
function getdate_Mdys_En() {
    return months+" "+date.getDate()+", "+date.getFullYear();
}
function getdate_Mdys_Ens() {
    return months+", "+date.getDate()+", "+date.getFullYear();
}
/*week Month day year*/
function getdate_WMdys_En() {
    return weekEns+","+months+" "+date.getDate()+", "+date.getFullYear();
}
/*week Month day year*/
function getdate_WMdy_En() {
    return weekEn+""+month+""+date.getDate()+", "+date.getFullYear();
}
/*Month day year*/
function getdate_Mdy_En() {
    return month+" "+date.getDate()+", "+date.getFullYear();
}

/* day  Month year*/
function getdate_dMys_En() {
    return date.getDate()+" "+months+", "+date.getFullYear();
}
/* day  Month year*/
function getdate_dMy_En() {
    return date.getDate()+" "+month+" "+date.getFullYear();
}

//未完待续...

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值