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 8911241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:53:32+00:00 2026-06-15T03:53:32+00:00

I am trying to sort date-time strings using Regex in my javascript code but

  • 0

I am trying to sort date-time strings using Regex in my javascript code but am running into a problem. If the HH portion of the string “MM/DD/YYYY HH:MM:SS” is one digit, my program sorts that with a missing digit and is therefor a way smaller number and does not sort properly.

My regex is this (the part in question is in bold):

/^(\d{ 1,2 })[/- ](\d{ 1,2 })[/- ](\d{ 4 })[\s](\d{ 1, 2})[\:](\d{ 2 })[\:](\d{ 2 })[\s]([ AP]M)?/g

Is there a way to add a zero to the front of the HH if the number is one digit? And without using any .replace() methods, because that wont work in the section of my sort function.

Thanks in advance!

  • 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-06-15T03:53:34+00:00Added an answer on June 15, 2026 at 3:53 am

    You can’t modify the string without using replace. You can “normalize” a date that matches your regex:

    var out = old
    .replace(/^(\d\d?)[\/ -](\d\d?)[\/ -](\d{4})\s(\d\d?):(\d\d):(\d\d)(?:\s([AP]M))?/g
    ,function(x,m,d,y,h,i,s,a) {
        if( m.length == 1) m = "0"+m;
        if( d.length == 1) d = "0"+d;
        if( a == "PM") h = ""+((h%12)+12);
        if( a == "AM" && h == 12) h = "0";
        if( h.length == 1) h = "0"+h;
        return y+m+d+h+i+s;
    });
    

    For today’s date, this would return:

    20121130141320
    

    The date components are sorted from biggest to smallest, which means a simple .sort() call would arrange the dates in the right order with no fuss.

    NOTE: I edited your regex a little, but it still matches the same stuff. The only difference is that now if there is no AM or PM, the space at the end is no longer required.

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

Sidebar

Related Questions

I'm trying to sort by date then start time. Start time is minutes from
I am trying to sort an array in PHP by date and time which
I'm trying to build a javascript array of objects, parsing dates using date.js, and
I'm trying to sort a table with javascript Date objects in one of the
I am trying to sort my posts in my discussion board by date. Here
I am trying to sort a list of names followed by another string such
I am trying to sort a list of records that have been created using
I am trying to create a website using some of the Razor functionality, but
I'm trying to sort a list of files by date. I currently have a
I'm trying to create an array, and then sort the objects by time, from

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.