How to Convert Dd/Mm/Yyyy String into JavaScript Date Object

How to convert dd/mm/yyyy string into JavaScript Date object?

MM/DD/YYYY format

If you have the MM/DD/YYYY format which is default for JavaScript, you can simply pass your string to Date(string) constructor. It will parse it for you.

var dateString = "10/23/2015"; // Oct 23
var dateObject = new Date(dateString);
document.body.innerHTML = dateObject.toString();

How to convert a dd/mm/yyyy string to a date in Javascript

You need to enter the date in the format of "01/28/2019" for it to be a valid date string which can be parsed. You can do this using .split() to manipulate the string around.

See example below:

var date1 = "28/01/2019".split('/')var newDate = date1[1] + '/' +date1[0] +'/' +date1[2];
var date = new Date(newDate);console.log(date);

Convert dd-mm-yyyy string to date

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)
}

How to convert a dd.MM.yyyy string to a Date object

If you absolutely have to have it in that format, you can rearrange it quick before using the date object as it expects a certain format..

Something like this could work.

if( typeof myVar === 'string') {
let dateArr = myVar.split('.');

let myDate = new Date(dateArr[1] + '-' + dateArr[0] + '-' + dateArr[2]);

if (myDate.getTime() < this.getMinDate().getTime()) /** compare two dates */
//omitted source code

}

new Date('dd/mm/yyyy') instead of newDate('mm/dd/yyyy')

You have no control over the Date constructor, so you need to feed it a date in the format that it wants. Since you are formatting the date yourself, it is better to use the other Date constructor, which takes the year, monthIndex, and day as arguments, since it is more bullet-proof across different browsers and runtimes:

  function my_date(date_string) {      var date_components = date_string.split("/");      var day = date_components[0];      var month = date_components[1];      var year = date_components[2];      return new Date(year, month - 1, day);    }        console.log(my_date("03/01/2018"));

Convert dd-mm-yy to javascript date object

Ok, so here is how I solved it.

The answers here is right, except I wasn't happy with the way the detection of the current century worked; so I basically did this:

var date = new Date(year + 2000, month - 1, day);

if (date > new Date())
date.setYear(year + 1900);

With this code, I can maximum validate an age of 100, but that shouldn't be a problem.

Mukul Goel in the comment below points out it can only validate dates in the future. Probably right, I haven't checked it myself.



Related Topics



Leave a reply



Submit