Geeks With Blogs
Jenson Code Ranch Code Ranch for Web and Mobile Development

I often find it hard to convert a date string into the date I desired. I'm not sure is it only me or any of you encounter the same problem before. Whenever I tried to do something like:

var myDate = new Date(dateString);

I will always get Today's date, instead of the value i stored in the dateString variable. And most of the time, when people show you sample codes on how to make comparison between date string by converting them into Date, it normally won't work for me.

One of the reason could be I actually get the date string as dd-MMM-yyyy format, for example, if today's date is 4th August 2011, I will get 04-Aug-2011, that's for easy reading. What if you have a Jan 5, 2011 displayed as 05-01-2011, so without knowing the locale, what is your guess? Is it Jan 1, 2011 or May 1, 2011? So 05-Jan-2011 would be clearer if your actual date is Jan 5, 2011.

My methods involve more steps, but at least it's working for me.

This is what I did in the <script> block of my ASP.NET Source:
        
   //I actually put this in a function
   var toDate = document.getElementById('ctl00_ContentPlaceHolder1_txtDateTo')

            var frmDate = document.getElementById('ctl00_ContentPlaceHolder1_txtDateFrom')

            var today = new Date();

            Date.prototype.getJulian = function() {
                return Math.floor((this / 86400000) -
                (this.getTimezoneOffset() / 1440) + 2440587.5);
            }
   
   //To convert from a Date variable, it take more steps, if I try to reduce the steps
   //it will give me the value NaN. I noticed I can't do it this way:
   // var tmpToday = today.format('dd-MMM-yyyy');
   // var julToday = new Date(getDate(tmpToday)).getJulian();
   // These 2 lines are what get me a 'NaN' value
   
   //The actual working codes
   var tmpToday = today.format('dd-MMM-yyyy');
            var tmpjulToday = new Date(getDate(tmpToday));
            var julToday = tmpjulToday.getJulian();
   
            var julToday = today.getJulian();

            var tmpFromDate = new Date(getDate(frmDate.value));
            var tmpToDate = new Date(getDate(toDate.value));

            var julFromDate = tmpFromDate.getJulian();
            var julToDate = tmpToDate.getJulian();
   //End of the function
   
  //Function to convert Date format to M/d/Y format 
  function getDate(inputDtStr) {

            var pos1 = inputDtStr.indexOf(dtCh)
            var pos2 = inputDtStr.indexOf(dtCh, pos1 + 1)
            var strDay = inputDtStr.substring(0, pos1)
            var strMonth = inputDtStr.substring(pos1 + 1, pos2)
            var strYear = inputDtStr.substring(pos2 + 1)
            strYr = strYear

            switch (strMonth) {
                case 'Jan':
                    strMonth = "01";
                    break;
                case 'Feb':
                    strMonth = "02"
                    break;
                case 'Mar':
                    strMonth = "03"
                    break;
                case 'Apr':
                    strMonth = "04"
                    break;
                case 'May':
                    strMonth = "05"
                    break;
                case 'Jun':
                    strMonth = "06"
                    break;
                case 'Jul':
                    strMonth = "07"
                    break;
                case 'Aug':
                    strMonth = "08"
                    break;
                case 'Sep':
                    strMonth = "09"
                    break;
                case 'Oct':
                    strMonth = "10"
                    break;
                case 'Nov':
                    strMonth = "11"
                    break;
                case 'Dec':
                    strMonth = "12"
                    break;
            }

            if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
            if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
            for (var i = 1; i <= 3; i++) {
                if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
            }
            month = parseInt(strMonth)
            day = parseInt(strDay)
            year = parseInt(strYr)

            var dateVal = month + "/" + day + "/" + year

            return dateVal

        }
  
Why I need Julian Date function? That is because, if I just get the today.getJulian(), and compare with frmDate value, I would always get the result that my frmDate value is earlier than today's date, even though I actually set the frmDate value to be the same as Today's date. Why? Because when you declare a new Date() variable, it actually includes the timestamp, so when you use getJulian() on it, it will get you the Julian date numeric value including the timestamp, which is of course always earlier than your formatted date, e.g. 04-Aug-2011, which is without the timestamp.

Do you have better solution? Please kindly share with me and the rest and we shall make this solution available to everyone who needs it!

Enjoy!

Posted on Thursday, August 4, 2011 5:18 PM | Back to top


Comments on this post: [JavaScript in ASP.NET] How to convert the input date to Julian Date for Date Comparison?

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Jenson | Powered by: GeeksWithBlogs.net