JS-FCC algorithm friendly Date Ranges

Source: Internet
Author: User
Tags date1 month name ranges

Make the date range more friendly!

Convert a common date format, such as: to YYYY-MM-DD an easier to read format.

The easy-to-read format is to use the month name instead of the month number and the ordinal number to represent the day ( 1st instead 1 ).

Remember not to show the information that can be inferred: if the end date in a date range is less than one year from the start date, the end date is not written for the year. Month start and end date if the same one month, the end date month is not written.

Also, if the start date year is the current year and the end date is less than one year from the start date, the year of the start date is not written.

My Code:

1 functionmakefriendlydates (arr) {2   //get the current year3   varYearnow= (NewDate ()). getFullYear ();4   //Put the passed-in arguments into an array of strings, creating a date type can also5   varDate1=arr[0].split ("-");6   varDate2=arr[1].split ("-");7   //The English expression of the month8   varmonths=["January", "February", "March", "April", "may", "June", "July", "August", "September", "October", "November", " December "];9   //initialize several arrays that are used laterTen   varDate1str= ""; One   varDate2str= ""; A   varDatearr=[]; -   //a function to add a suffix to a date -   functionfriendlydate (str) { the     varstr2num=Number (str); -     Switch(str2num) { -        Case1: -str2num+= "St"; +          Break; -        Case2: +str2num+= "nd"; A          Break; at        Case3: -str2num+= "RD"; -          Break; -       default: -str2num+= "TH"; -     } in     returnStr2Num; -   } to   //The string representation of a date1 is in most cases a date that takes years. Date2 if not in the same month, in most cases it will take a month. +date1str=months[date1[1]-1]+ "" +friendlydate (date1[2]) + "," +date1[0]; -   if(date1[1]===date2[1]&&date1[0]===date2[0]){ theDate2str=friendlydate (date2[2]); *}Else{ $date2str=months[date2[1]-1]+ "" +friendlydate (date2[2]);Panax Notoginseng   } -   //if more than one year, Date2 plus year, if less than one year, and Date1 date is this year, then remove date1 year.  the   if((date2[0]-date1[0]>1) | | ((date2[0]-date1[0]===1) && (date2[1]-date1[1]>0)) | | ((date2[0]-date1[0]===1) && (date2[1]-date1[1]===0) &&date2[2]-date1[2]>=0)){ +date2str+= "," +date2[0]; A}Else if(date1[0]==Yearnow) { theDate1str=date1str.slice (0,-6); +   } -   //Put two dates in the same array output (if the same day in the same month, the code date2str useless, so the code can be improved).  $datearr[datearr.length]=Date1str; $   if(Date1.tostring ()!==date2.tostring ()) { -datearr[datearr.length]=Date2str; -   } the   returnDatearr; -}

In special cases, DATE2STR is useless and there is no time to improve.

JS-FCC algorithm friendly Date Ranges

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.