JavaScript some formatting date methods

Source: Internet
Author: User
Tags dateformat

These methods are written at the time of the project, some may not be well written, but they can be collected as a date formatting tool.

varDateutils = {/** * Formatted date * @param date * @returns {string} */Getformatday:function (date) {varDateTime;varDateFormat;//Var flag=date.indexof ("-");        if(typeof date = ="Object") {DateTime =NewDate (date); }Else{DateTime =NewDate (Date.replace (/-/g,'/')); }//----Change the time format to such as 2014-01-01----        varMon2 = (number (Datetime.getmonth ()) +1);varDay2 = Datetime.getdate ();if(Mon2 <Ten) {Mon2 ="0"+ Mon2; }Else{Mon2 = Mon2 +""; }if(Day2 <Ten) {day2 ="0"+ day2; }Else{day2 = Day2 +""; } DateFormat = Datetime.getfullyear () +"-"+ Mon2 +"-"+ day2;returnDateFormat; },/** * Format time, switch to "day + hour + minutes + seconds" * @param Longtime unit: SEC */Getformattime:function (longtime) {varTime = parsefloat (longtime);if(Time! =NULL&& Time! ="") {if(Time < -) {vars = time; Time = s +"Seconds"; }Else if(Time >= -&& Time <3600) {varm = parseint (Time/ -);vars = parseint (time% -); Time = m +"Minutes"+ S +"Seconds"; }Else if(Time >=3600&& Time <86400) {varh = parseint (Time/3600);varm = parseint (time%3600/ -);vars = parseint (time%3600% -% -); Time = h +"Hours"+ M +"Minutes"+ S +"Seconds"; }Else if(Time >=86400) {varD = parseint (Time/86400);varh = parseint (time%86400/3600);varm = parseint (time%86400%3600/ -);vars = parseint (time%86400%3600% -% -); Time = d +"Day"+ H +"Hours"+ M +"Minutes"+ S +"Seconds"; }        }returnTime },/** * Get this morning's time (in seconds) */Gettodaytime:function () {varToday =NewDate ();varStryear = Today.getfullyear ();varStrday = Today.getdate ();varStrmonth = Today.getmonth () +1;if(Strmonth <Ten) Strmonth ="0"+ Strmonth;if(Strday <Ten) Strday ="0"+ strday;varStrtoday = Stryear +"-"+ Strmonth +"-"+ strday;varStrtodaytime = Strtoday +"00:00:00";varTodayDate =NewDate (Strtodaytime.replace (/-/g,'/'));returnTodaydate.gettime (); },/** * Get yesterday's date */Getproxday:function () {varToday =NewDate ();varYesterday_milliseconds = Today.gettime ()- +* -* -* -;varYesterday =NewDate (); Yesterday.settime (Yesterday_milliseconds);varStryear = Yesterday.getfullyear ();varStrday = Yesterday.getdate ();varStrmonth = Yesterday.getmonth () +1;if(Strmonth <Ten) Strmonth ="0"+ Strmonth;if(Strday <Ten) Strday ="0"+ strday;varStryesterday = Stryear +"-"+ Strmonth +"-"+ strday;varStryesterdaytime = Stryesterday +"00:00:00";varProxdate =NewDate (Stryesterdaytime.replace (/-/g,'/'));returnProxdate; }};

dateUtils.getProxDay()
Fri Jul 00:00:00 gmt+0800 (China Standard Time)
dateUtils.getTodayTime()
1435939200000
dateUtils.getFormatDay(new Date())
"2015-07-04"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JavaScript some formatting date methods

Related Article

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.