Question

I have inputs with values that have date format like dd.mm.yy.

I get them:

var date1 = $('.main-order-form input[name=start_date]').val();
var date2 = $('.main-order-form input[name=end_date]').val();

And want to make them like yyyy, MM, dd (it's need to compare them)

I try this, but nothing changed.

console.log(date1.toString("yyyy, mm, dd"));

1) How to change date format?

2) Is it posible to count weekends days between two dates?

Was it helpful?

Solution

See this post on formatting the dates:https://stackoverflow.com/a/1056730/2033671

to get a date object in your case you can split the val and create a new date.

var date1 = $('.main-order-form input[name=start_date]').val().split(".");
date1 = new Date(date1[1] + "." + date1[0] + "." + date1[2]);

your second question, is a second question! Try some stuff out and ask when you get stuck

OTHER TIPS

The problem is that when you are reading the dates from the inputs you are getting string values. Calling toString on these with a date pattern will not work. As mentioned in a comment you are best using a date library to achieve this.

Have a look at date.js or moment.js

This is an example using date.js:

var date1 = Date.parse($('.main-order-form input[name=start_date]').val())
date1.toString("yyyy, mm, dd")

With plain Javascript you have to change the format manually:

seperatedDateStrings = "dd.mm.yy".split(".");
bla = new Date(
  seperatedDateStrings[2], 
  seperatedDateStrings[1],
  seperatedDateStrings[0]
);
newDateString = bla.getFullYear() + ", " + bla.getMonth() + ", " bla.getDate();

I don't know if exists a jquery plugin that does the work for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top