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

  • SEARCH
  • Home
  • 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 9249869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:18:20+00:00 2026-06-18T10:18:20+00:00

I have this date parsed from an api as a string: DD-MM-YYYY but sometimes

  • 0

I have this date parsed from an api as a string:
DD-MM-YYYY but sometimes the date is DD-M-YYYY or even D-M-YYYY.

For example:

4-1-2013
or
10-10-2013
or 7-4-2013

The year is always 4 digits but days or months sometimes get one digit. How can I manually (with JS) add 0 in front of a every single digit ?

I am using moment.js for some calculations thus I am remove the ‘-‘ using

date.replace("-", "")

to get a whole number (eg. 4-1-2013 = 412013) so I can use it with moment.js but if its a single digit, it all gets messed up.

  • 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-18T10:18:21+00:00Added an answer on June 18, 2026 at 10:18 am

    You can normalise your strings first like this:

    date = date.replace(/\b(\d{1})\b/g, '0$1');
    

    which will take any “word” that is just a single digit and prepend a zero.

    Note that to globally replace every - you must use the regexp version of .replace – the string version you’ve used only replaces the first occurrence, therefore:

    date = date.replace(/\b(\d{1})\b/g, '0$1').replace(/-/g, '');
    

    will do everything you require.

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

Sidebar

Related Questions

I have a date string 2011-11-24T09:00:27+0000 fetched from the graph.facebook API. When I run
I have this code snippet: DateFormat formatter1; formatter1 = new SimpleDateFormat(mm/DD/yyyy); System.out.println((Date)formatter1.parse(08/16/2011)); When I
I have been passed a date string that looks like this: Thu%20Mar%2011%202010%2015%3A09%3A11%20GMT%2B0000%20(BST) I want
I have this code for checking whether the Date is OK or not, but
I have date formatted in this way: 2010-02-04T00:00:00 I use this code to parse
i have this date's & Time's: date1: 10/04/2010 - Time: 08:09 date2: 11/04/2010 -
I have this: var date = new DateTime(2009, 12, 5); ... ... and need
I have something like this: [{'date': 1, 'value':5}, {'date':2,'value':3}, ...] and want to map
Have this query: SELECT HOUR( DATE ) AS hr, COUNT( * ) AS cnt
I have this form with a date input. echo $this->Form->create('Nodata'); echo $this->Form->input('date1', array('type' =>

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.