3

I want to be able to enter a UK formatted date when creating a new Date from a string, that will work in all modern browsers, but mainly FireFox and IE.

new Date('24/06/2014') // Gives 06/24/2014

Because I want to set a jquery datepicker using it.

Is it possible to get

new Date()

to interpret a UK date passed as a string?

new Date('24/06/2014').toLocaleString("en-GB") // Gives 06/12/2015

new Date('06/24/2014').toLocaleString("en-GB") // Gives 24/06/2014

The real problem is when I use the datepicker I specify UK date format and everything is fine...

$("#outOfStockFromDate").datepicker({
    dateFormat: 'dd/mm/yy',
changeMonth: true,
numberOfMonths: 3,
onClose: function (selectedDate) {
    $("#to").datepicker("option", "minDate", selectedDate);
}});

Until I try to repopulate the datepicker using this UK date format using something like...

$("#outOfStockFromDate").datepicker("setDate", testDate);

It's as if the string 'testDate' must be in US format... grrr

EDIT: I realise there are two parts to this question, the first part dealing with datepicker; it was my stupid fault that I was initialising it at the same time I was trying to setDate (this error led me down the Date() route). Now I have it initialised and changed correctly it accepts UK format dates!

Second part yes the date functionality in javascript regarding formatting dates (now I know) is famously lacking... but as I've solved my initial problem I don't care about this one... P.S. I ended up splitting and reconstructing the date in US Date Format (in a string) then using new Date(USDateFormattedString).... and then I realised!

So to end, essentially my code was correct in it's syntax, but in it's execution it was not.

Thanks to everyone who answered! +1

Paul Zahra
  • 9,522
  • 8
  • 54
  • 76
  • I dont know if this might help , but will this work `var now = new Date(); now.format("dd/m/yy");` ?, using [this](http://blog.stevenlevithan.com/archives/date-time-format) library – Mustafa sabir Jun 24 '14 at 12:39
  • Thanks but no it won't work for what I want... I need to have new Date('24/06/2014') Give 24/06/2014 not 06/12/2015 which is what it currently thinks. – Paul Zahra Jun 24 '14 at 13:20

2 Answers2

7

You can't get new Date to accept a string with a date in UK format directly, however, you can parse it manually as I've shown in another question with similar issue:

function parseDMY(value) {
    var date = value.split("/");
    var d = parseInt(date[0], 10),
        m = parseInt(date[1], 10),
        y = parseInt(date[2], 10);
    return new Date(y, m - 1, d);
}

This version as is accepts dates like this one:

parseDMY("01/13/2014").toLocaleString("en-GB")
"1/1/2015 00:00:00"

You can add some validations if you want to make sure that doesn't happen.

Community
  • 1
  • 1
izstas
  • 5,004
  • 3
  • 42
  • 56
1

Assuming you are using jquery-ui then I believe your missing the reference to the localization of the datepicker. For example:

$("#datepicker").datepicker( "option",$.datepicker.regional[ "en-GB" ] );

Here is a complete example:

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Localize calendar</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
</head>
<body>

<p>Date: <input type="text" id="datepicker">&nbsp;
<button id="set">Test Set Date</button>
<button id="get">Test Get Date</button>

</body>
<script>
  $(function() {
  $( "#datepicker" ).datepicker({ dateFormat: "dd/mm/yy" });
  $( "#datepicker" ).datepicker( "option",
  $.datepicker.regional[ "en-GB" ] );

  $("#set").click(function(){
    $("#datepicker").datepicker("setDate", new Date());
  });
  $("#get").click(function(){
    alert($("#datepicker").datepicker("getDate"));
  });
});
</script>
</html>

Here is the documentation of jQuery UI localization.

All the best, acg

acg
  • 961
  • 5
  • 10
  • Does it work for you? I've tried it and it doesn't work without the dateFormat "dd/mm/yy' which makes me wonder what the hell 'regional' is doing... The default region is English anyhow, and to set it to English you use empty quotes... Lastly in their documentation it is set using $.datepicker.setDefaults( $.datepicker.regional[ "" ] ); (Which by the way also makes not one jot of difference to the basic date format displayed, maybe it does under the scenes) http://api.jqueryui.com/datepicker/ Thanks anyway! ;) – Paul Zahra Jun 24 '14 at 14:01
  • Putting politics aside, I don't trust what localization wins for "english" since I dont know if that is en-US (Yanks) or en-GB (Jacks), but I do agree that the formatting of dates is bit off and could improved upon. That being said, it works for me when I add the format and out of "best" practices I would add the localization, without any issue of mm/dd. – acg Jun 24 '14 at 14:18