Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6107813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:13:45+00:00 2026-05-23T14:13:45+00:00

I have the following code: function parseDate(s) { var date = new Date(s); if

  • 0

I have the following code:

function parseDate(s) {
var date = new Date(s);
if (!isValidDate(date)) {
    //iso 860 date parser, as some browsers do not support this via new Date yet
    var re=/(\d\d\d\d)\D?(\d\d)\D?(\d\d)\D?(\d\d)\D?(\d\d\D?(\d\d\.?(\d*))?)(Z|[+-]\d\d?(:\d\d)?)?/;
    var a=re(s).slice(1).map(function(x,i){
        if (i==6 && x) x=parseInt(x,10)/Math.pow(10,x.length)*1000; // convert to milliseconds
        return parseInt(x,10)||0;
    });
    date = new Date(Date.UTC(a[0],a[1]-1,a[2],a[3]-(a[7]||0),a[4],a[5],a[6]));
}
return date;
};

function isValidDate(d) {
  if ( Object.prototype.toString.call(d) !== "[object Date]" )
       return false;
  return !isNaN(d.getTime());
}

This works in all browsers but IE6-9. There I get the error:

SCRIPT5002: Function expected

And it points to this line:

        var a=re(s).slice(1).map(function(x,i){

Anyone know what is wrong with that and how to fix it?

Thanks,
Wesley

Edit:

if I change the code to this:

function parseDate(s) {
var date = new Date(s);
if (!isValidDate(date)) {
    //iso 860 date parser, as some browsers do not support this via new Date yet
    var re=/(\d\d\d\d)\D?(\d\d)\D?(\d\d)\D?(\d\d)\D?(\d\d\D?(\d\d\.?(\d*))?)(Z|[+-]\d\d?(:\d\d)?)?/;
    var a = re.exec(s);
    if (a) { 
        a = a.slice(1);
        a.map(function(x,i){
            if (i==6 && x) x=parseInt(x,10)/Math.pow(10,x.length)*1000; // convert to milliseconds
            return parseInt(x,10)||0;
        });
        document.getElementById('test1').innerHTML = (a[0] + ' ' + a[1] + ' ' + a[2] + ' ' + a[3] + ' ' + a[4] + ' ' + a[5] + ' ' + a[6] + ' ' + a[7]);
        date = new Date(Date.UTC(a[0],a[1]-1,a[2],a[3]-(a[7]||0),a[4],a[5],a[6]));
    }
 }
 return date;
};

It still doesn’t work (complains about .map in IE) but interestingly also doesn’t work in safari (and perhaps other browsers). Any reason why?

You’ll note that the output (document.write from the new function is:

2008 11 01 20 39:57.78 57.78 78 -06:00

From the old function:

2008 11 1 20 39 57 780 -6

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:13:46+00:00Added an answer on May 23, 2026 at 2:13 pm

    Regarding .map

    The .map() array method is one of the new additions provided by ECMAScript 5; the latest official version of JavaScript (5.0 as December 2009 and now 5.1 as of June 2011). Few (if any) browsers fully support version 5 yet (and certainly not IE6). You can bet that it will be a while before you can rely on browsers to support all the new functionalities (such as Array.map()).

    Regarding the Date regex

    Unfortunately JavaScript does not allow regexes to be specified using free-spacing mode. This makes longish regexes such as this one difficult to read. To shed some light on the reason why your algorithm isn’t working, I’ve added comments to your regex, which indicate what the capture groups are really capturing. Here it is in PHP syntax:

    $re_iso8601_date_needs_work = '%# Original Date regex
        (\d\d\d\d)\D?  # $1: Year.
        (\d\d)\D?      # $2: Month.
        (\d\d)\D?      # $3: Day.
        (\d\d)\D?      # $4: Hour.
        (              # $5: Minute and second. ???
          \d\d\D?      # Minute.
          (            # $6: Optional second.
            \d\d\.?    # Second (whole portion).
            (\d*)      # $7: Second (fractional portion).
          )?           # Second is optional.
        )              # End $5: minute and second.
        (              # $8: Optional timezone alternatives.
          Z            # Either UTC/Zulu.
        | [+-]\d\d?    # Or offset from UTC. Hours and
          (:\d\d)?     # $9: optional minutes.
        )?             # Timezone is optional.%x';
    

    Hint: The fractional seconds are in $7 not $6. And why are you capturing both minutes and seconds in group $5?

    Hope this helps! 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: function reload() { var referenceID = $('#ReferenceID').val(); $('#detailData').load(Url.Action(DetailData, new
I currently have the following js code function clearMulti(option) { var i; var select
I have the following jquery code: jQuery(function(){ jQuery(select#rooms).change(function(){ var options = ''; jQuery.getJSON(/admin/selection.php,{id: jQuery(this).val(),
given i have the following block of code (function(){ var mb = { abc:function(){
I have the following code: $(document).ready(function() { var id = 'cbx'; var idPrefix =
I have the following bit of code: protected function onEnterFrame(e:Event):void { var diff:Number; //
I have the following code: function header(){ experience += ''; var expimage = '';
If I have the following code: function foo() { var a = []; for(var
I have the following code (function($) { // carousel var Carousel = { settings:
I have the following code: function computeSetImgMargins(iD) { var ids = iD; var totalWidth

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.