Some JavaScript date formatting methods, javascript date

Source: Internet
Author: User
Tags dateformat

Some JavaScript date formatting methods, javascript date

These methods are all written during project creation. Some of them may not be well written, but they can be collected as a date formatting tool.

Var dateUtils = {/*** format date * @ param date * @ returns {string} */getFormatDay: function (date) {var dateTime; var dateFormat; // var flag = date. indexOf ("-"); if (typeof date = "object") {dateTime = new Date (date);} else {dateTime = new Date (date. replace (/-/g, '/');} // --- change the time format to 2014-01-01 ---- var mon2 = (Number (dateTime. getMonth () + 1); var day2 = dateTime. getDate (); if (mon2 <10) {mon 2 = "0" + mon2;} else {mon2 = mon2 + "";} if (day2 <10) {day2 = "0" + day2 ;} else {day2 = day2 + "";} dateFormat = dateTime. getFullYear () + "-" + mon2 + "-" + day2; return dateFormat ;},/*** format time, convert to "day + hour + minute + second" * @ param longTime unit: Second */getFormatTime: function (longTime) {var time = parseFloat (longTime); if (time! = Null & time! = "") {If (time <60) {var s = time; time = s + "seconds";} else if (time >=60 & time <3600) {var m = parseInt (time/60); var s = parseInt (time % 60); time = m + "Minute" + s + "second ";} else if (time> = 3600 & time <86400) {var h = parseInt (time/3600); var m = parseInt (time % 3600/60 ); var s = parseInt (time % 3600% 60% 60); time = h + "Hour" + m + "Minute" + s + "second ";} else if (time> = 86400) {var d = parseInt (time/86400); var h = parseInt (time % 86400/3600); var m = parseInt (time % 86400% 3600/60 ); var s = parseInt (time % 86400% 3600% 60% 60 ); time = d + "day" + h + "Hour" + m + "Minute" + s + "second" ;}} return time ;}, /*** get the early morning time (in seconds) */getTodayTime: function () {var today = new Date (); var strYear = today. getFullYear (); var strDay = today. getDate (); var strMonth = today. getMonth () + 1; if (strMonth <10) strMonth = "0" + strMonth; if (strDay <10) strDay = "0" + strDay; var strToday = strYear + "-" + strMonth + "-" + strDay; var strTodayTime = strToday + "00:00:00"; var todayDate = new Date (strTodayTime. replace (/-/g, '/'); return todayDate. getTime () ;},/*** get the Date of yesterday */getProxDay: function () {var today = new Date (); var yesterday_milliseconds = today. getTime ()-1000*60*60*24; var yesterday = new Date (); yesterday. setTime (yesterday_milliseconds); var strYear = yesterday. getFullYear (); var strDay = yesterday. getDate (); var strMonth = yesterday. getMonth () + 1; if (strMonth <10) strMonth = "0" + strMonth; if (strDay <10) strDay = "0" + strDay; var strYesterday = strYear + "-" + strMonth + "-" + strDay; var strYesterdayTime = strYesterday + "00:00:00"; var proxDate = new Date (strYesterdayTime. replace (/-/g, '/'); return proxDate ;}};

dateUtils.getProxDay()
Fri Jul 03 2015 00:00:00 GMT + 0800 (Chinese Standard Time)
dateUtils.getTodayTime()
1435939200000
dateUtils.getFormatDay(new Date())
"

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.