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

The Archive Base Latest Questions

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

i am trying to convert a string in the format dd-mm-yyyy into a date

  • 0

i am trying to convert a string in the format dd-mm-yyyy into a date object in JavaScript using the following:

 var from = $("#datepicker").val();
 var to = $("#datepickertwo").val();
 var f = new Date(from);
 var t = new Date(to);

("#datepicker").val() contains a date in the format dd-mm-yyyy.
When I do the following, I get “Invalid Date”:

alert(f);

Is this because of the ‘-‘ symbol? How can I overcome this?

  • 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-25T01:45:14+00:00Added an answer on May 25, 2026 at 1:45 am

    Split on “-“

    Parse the string into the parts you need:

    var from = $("#datepicker").val().split("-")
    var f = new Date(from[2], from[1] - 1, from[0])
    

    Use regex

    var date = new Date("15-05-2018".replace( /(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3"))
    

    Why not use regex?

    Because you know you’ll be working on a string made up of three parts, separated by hyphens.

    However, if you were looking for that same string within another string, regex would be the way to go.

    Reuse

    Because you’re doing this more than once in your sample code, and maybe elsewhere in your code base, wrap it up in a function:

    function toDate(dateStr) {
      var parts = dateStr.split("-")
      return new Date(parts[2], parts[1] - 1, parts[0])
    }
    

    Using as:

    var from = $("#datepicker").val()
    var to = $("#datepickertwo").val()
    var f = toDate(from)
    var t = toDate(to)
    

    Or if you don’t mind jQuery in your function:

    function toDate(selector) {
      var from = $(selector).val().split("-")
      return new Date(from[2], from[1] - 1, from[0])
    }
    

    Using as:

    var f = toDate("#datepicker")
    var t = toDate("#datepickertwo")
    

    Modern JavaScript

    If you’re able to use more modern JS, array destructuring is a nice touch also:

    const toDate = (dateStr) => {
      const [day, month, year] = dateStr.split("-")
      return new Date(year, month - 1, day)
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert a string into money format using this function , and
I am trying to convert a string of the below format dd/MM/yyyy hh:mm:ss.fff into
I am trying to convert a date into this format YYYY-MM-DD hh:mm:ss eg. 2007-01-05
I'm trying to convert a string into a date format My string looks like
I am trying to convert a string to proper date format using Java's SimpleDateFormat
I am trying to convert a string into date time format. DateTime.Parse(tempfrmBankDetails.dgvBankDetails.SelectedRows[0].Cells[PaymentDate].Value.ToString(),null); This is
I'm trying to convert a string from to a Joda DateTime object. The date
I am trying to convert a date/time string in this format YYYY/MM/DD/HH:MM:SS to a
I'm trying to convert the String \something\ into the String \\something\\ using replaceAll ,
I'm trying to convert a date string into an age. The string is like:

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.