Javascript, advancing date range

I using a date-picker to set a start and end date. The date range is supposed to move forward each day for a two week, sliding window of entry.

My code below works but I would like to refactor it into something that is easier to maintain. What techniques or tools can I use to improve this code?

var challenge_start = new Date(2015,04,04); var challenge_end = new Date(2015,06,12); var range_start; var range_end; var today = new Date(); var dd = today.getDate(); var mm = today.getMonth()+1; //January is 0! var yyyy = today.getFullYear(); if(today<challenge_start) { range_start=challenge_start; range_end=challenge_start; } else if(today>challenge_start && today<challenge_end) { if(mm==5) { if(dd<18) { range_start=challenge_start; range_end=today; } else { range_start=today.setDate(today.getDate()-14); range_end=today; } } else if(mm=6) { if(dd<29) { range_start=today.setDate(today.getDate()-14); range_end=today; } else { range_start=today.setDate(today.getDate()-14); range_end=challenge_end; } } else if(mm==7) { if(dd<12) { range_start=today.setDate(today.getDate()-14); range_end=challenge_end; } else { range_start=challenge_end; range_end=challenge_end; } } } else if(today>challenge_end) { range_start=challenge_end; range_end=challenge_end; } $("#date").datepicker({ startDate: range_start, endDate: range_end, keyboardNavigation: false, autoclose: true, todayHighlight: true, language: langdate, disableTouchKeyboard: true });

-------------Problems Reply------------

I may not understand what you are striving for, but I'm pretty sure you don't need to have those if(mm==x) and if(dd=x) statements.

Perhaps this is what you want:

var challenge_start = new Date(2015,04,04);
var challenge_end = new Date(2015,06,12);

function getDatePlusDays(origDate, days) {
var date = new Date(origDate.getTime());
date.setDate(date.getDate() + days);
return date;
}

function getClosestDateInRange(startDate, endDate, date) {
return (date < startDate) ? startDate
: ((date > endDate) ? endDate: date);
}

var today = new Date();
var range_start = getClosestDateInRange(challenge_start, challenge_end, getDatePlusDays(today, -14));
var range_end = getClosestDateInRange(challenge_start, challenge_end, today);

jsfiddle

Category:javascript Views:1 Time:2018-03-12

Related post

  • Date range selector similar to Google Analytics date range selector? 2011-01-09

    I'm building a web app where the vast majority of our target audience likely have experience with Google Analytics - and as a part of that website there is a date range selector. I was hoping to find an off-the-shelf javascript/jquery date range sele

  • How to calculate date range for week and month in javascript? 2010-07-18

    Can someone guide me on date range in JavaScript? I want to calculate one week and month date range from today's date; I.e, if today is "18th july 2010", the range for the week should be "11/07/2010 - 8/07/2010" and for the month it should be "01/07/

  • Struts: Highlighting Date Range in Calendar (JQuery/Javascript) 2012-03-19

    Thank you very much in advance for reading. I'm programming a web application in Struts. I'm using a JQuery datepicker 1.7 as a calendar on the sidebar on the user's view. (It's the best option I could find) I would like to highlight in the calendar

  • Does anyone know of a good pre-existing javascript date range drop-down? 2009-08-18

    I'm looking for a javascript drop-down for a date range that allows the user to select start and end dates from a single form field, similar to what exists in the Google Analytics UI. I've found a lot of pre-existing javascript for two separate form

  • javascript for enabling/disabling 2 date range/date in Cognos 2009-12-01

    I am looking for javascript for enabling/disabling two date range/date prompts in Cognos. Does anyone have any javascript for disabling/enabling two date range prompts? If I select the Date1 prompt, then the Date2 prompt has to be greyed out and vice

  • Date Range Using Advance Filter in Excel 2011-02-15

    I currently am using an advanced filter to pull data from a master list. I only have 1 column for the date in the master list. What I want to do is be able to enter a date range (1/1/11 - 1/31/11) and have the filter only pull the month of january ou

  • Date range restriction with JavaScript 2011-04-22

    There are two input fields of type text to write a start date and end date in format mm/dd/yyy. I need a JavaScript function to check that the date range interval between those entered dates does not exceed 14 days. And the maximum date should be the

  • javascript calculate price for date ranges 2012-03-06

    I have a question about javascript time ranges i have two input boxes, the client can give start date and end date below have 3 seasons with date ranges,i need to count the cost for the user entered date range with the mapping with below seasons with

  • JavaScript date ranges compare with another date range 2012-03-07

    I have a JavaScript problem. I have 3 seasons which has 5 date ranges: Season 1: 11th January to 30th April 1st July to 31st August Season 2 1st May to 30th June 1st September to 14th December Season 3 15th December to 10th January So I need a build

  • Filter records based on Date Range + ASP.NET + Regex + Javascript 2010-05-13

    I need to filter data based on a date range. My table has a field Process date. I need to filter the records and display those in the range FromDate to ToDate. How do I write a function in VB.NET which can help me filter the data. Am I on the right t

  • Calculate date range using javascript 2011-01-27

    I use a date/time picker to select date and time: var d = new Date(); $('#timePicker').datetimepicker({ dateFormat: 'dd-mm-yyyy', timeFormat: 'hh:mm', separator: '@', minDate: new Date(), maxDate: new Date(), }); min and maxDate define the date range

  • date range filtering issue with Javascript 2012-10-18

    I want to filtering the date between two textbox, my problem is when I pick the date on the second box, it doesnt include the date that i choose. the code for textbox <label for="from">From</label> <input type="text" id="from" />

  • How can I show the month name instead of date range in wdCalendar jquery plugin? 2010-08-23

    I have recently started using the wdClendar jquery plugin for one of my intranet sites. It works great but there have been a couple requests made that I cannot seem to work out. I have the calendar defaulted to displaying a month view. When the page

  • min max date range selection in date range picker 2011-03-08

    I am using Date Range Picker Codes are here: <input type="text" name="sample" id="sample" class="date_css" value="Select a date" /> Query is here: <script type="text/javascript"> $(function(){ $('#sample').daterangepicker({ arrows:true, d

  • How to use date range picker along with ajax 2011-03-15

    I am using date range picker javascript library To select range of date from user $('#date_range').daterangepicker({ arrows:true, dateFormat: 'd-M-yy', rangeSplitter: 'to', datepickerOptions: { changeMonth: true, changeYear: true, minDate: new Date("

  • Generating a report by date range in rails 2008-09-26

    How would you go about producing reports by user selected date ranges in a rails app? What are the best date range pickers? edit in response to patrick : I am looking for a bit of both widget and active record advice but what I am really curious abou

  • Validating Date Range Produced By jQuery UI DatePicker 2008-11-25

    I am using the date picker provided by jQuery UI to select a date range that ends up in a single text input with something like 11/11/2008 - 12/05/2008. What would you recommend as far a validation (jQuery/JavaScript) on the client side as well as co

  • Determine Whether Two Date Ranges Overlap 2008-11-28

    Given two date ranges, what is the simplest or most efficient way to determine whether the two date ranges overlap? As an example, suppose we have ranges denoted by DateTime variables StartDate1 to EndDate1 and StartDate2 to EndDate2. --------------S

  • Google AppEngine: Date Range not returning correct results 2009-04-06

    Im trying to search for some values within a date range for a specific type, but content for dates that exist in the database are not being returned by the query. Here is an extract of the python code: deltaDays = timedelta(days= 20) endDate = dateti

  • Fastest way to check date range 2009-05-21

    I store events in SQLServer 2005 where the time the event occured is important and must be stored in the datebase. What is the fastest way to write the date range check in the where clause to ensure everything on that day is selected? Currently when

  • Displaying totals within a specified date range in Microsoft Access 2009-08-04

    Although I have experience with SQL and generating HTML reports with PHP, I'm a relative beginner with Microsoft Access. I'm currently using Microsoft Access 2007 to connect to MSSQL Server 2005. I have a table of Reports that looks something like th

  • FogBugz - Filter List by date range using BETWEEN 2009-09-11

    Is there a way in FogBugz to filter a list of cases which have been opened/resolved/closed between a given date range? Thanks in advance! --------------Solutions------------- Try searching for (in your FogBugz): Opened:"September/1/2009..today" See S

  • Filtering by custom date range in Django admin 2009-11-03

    Can Django admin site select entries by a custom date range, i.e. using two DateFields with AdminDateWidget? I know that there are date_hierarchy and list_filter properties, but they don't seem very useful when there are a lot of DB entries and you j

  • JavaScript - Validate Date 2009-11-19

    I have an HTML text field. I want to validate via JavaScript that the value entered is a valid date in the form of "MM/DD/YY" or "MM/D/YY" or "MM/DD/YYYY" or "MM/D/YYYY". Is there a function that does this? I sort of assumed there was something like

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.162 (s). 11 q(s)