0

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?

skywind
  • 892
  • 6
  • 22
  • 44
  • 1
    That's not an answer, but you can include [momentJS](http://momentjs.com/), will save you a lot of time. – drinchev May 15 '13 at 14:52

3 Answers3

1

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")
CeejeeB
  • 3,034
  • 4
  • 25
  • 32
1

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

Community
  • 1
  • 1
0

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.

Reporter
  • 3,897
  • 5
  • 33
  • 47
  • the date constructor acts wonky. When you use the date "23.07.89" it makes the date `Wed Aug 23 1989 00:00:00 GMT-0500 (CDT)` –  May 15 '13 at 15:06
  • Just realized were dealing with computers and thats an off by 1 error if i ever saw one! so for your solution to work you will have to do `seperatedDateStrings[1] - 1` –  May 15 '13 at 15:08