-1

I want to get day from date. Suppose my date is 03-08-2013 it is in d-mm-yyyy format so I just want to get dand that is 03 from above date so I try this code but it does not work

Note I want to do it without including any js

var date = '08-03-2013';
var d = new Date(date);
alert(d.getDate());
// 2nd way
alert(date.getDate());

it alert NaN. What is missing in this code? here is jsfiddel Link Jsfiddle Link

UPDATE

Azam Alvi
  • 6,918
  • 8
  • 62
  • 89
  • your `data` var is of type `String` not `Date`, You cannot call `getDate()` method on type `String` – fujy Sep 01 '13 at 12:55
  • What is wrong with the first way (`alert(d.getDate())`)? – putvande Sep 01 '13 at 12:58
  • @putvande it's alert NaN with first way – Azam Alvi Sep 01 '13 at 13:00
  • @putvande `new Date('03-08-2013')` doesn't parse the date correctly so it won't return what's expected. – JJJ Sep 01 '13 at 13:01
  • @putvande `d.getDate()` gives me `8` which is the month, not the day. – André Dion Sep 01 '13 at 13:01
  • What date is it you are looking for? The 3rd of August or the 8th of March? Because you are getting the 8th of March with your code. Your format needs to be `mm-dd-yyyy`. – putvande Sep 01 '13 at 13:03
  • @putvande No. It should be an [RFC2822 or ISO 8601 date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse). `mm-dd-yyyy` won't work reliably cross-browser. – JJJ Sep 01 '13 at 13:07
  • The 3rd of August I want to get – Azam Alvi Sep 01 '13 at 13:07

4 Answers4

2

Date parsing in JS (and many languages, for that matter) is problematic because when the input is a date string, it's fairly ambiguous what piece of data is what. For example, using your date (August 3, 2013) it could be represented as

  • 03-08-2013 (dd-mm-yyyy)
  • 08-03-2013 (mm-dd-yyyy)

However, given just the date string, there's no way to tell if the date is actually August 3, 2013 or March 8, 2013.

You should pass your date values independently to guarantee the date is correctly parsed:

var
    str = '08-03-2013',
    parts = str.split('-'),
    year = parseInt(parts[2], 10),
    month = parseInt(parts[1], 10) - 1, // NB: month is zero-based!
    day = parseInt(parts[0], 10),
    date = new Date(year, month, day);

alert(date.getDate()); // yields 3
André Dion
  • 21,269
  • 7
  • 56
  • 60
2

Use moment.js. It's parsing ability is much more flexible than the Date class.

var m = moment('03-08-2013','DD-MM-YYYY');
var dayOfMonth = m.date();
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
1

You can't know the regional settings of your visitors.

If you know the format of the string is always d-mm-yyyy then just parse the value yourself:

function GetDay(rawValue) {
    var parts = rawValue.split("-");
    if (parts.length === 3) {
        var day = parseInt(parts[0], 10);
        if (!isNaN(day))
            return day;
    }
    alert("invalid date format");
    return null;
}

Live test case.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

Use this it that which you want..

var date = '08-03-2013';
date=date.replace(/([0-9]{2})\-([0-9]{2})\-([0-9]{4})/g, '$3-$2-$1');
var d = new Date(date);   
alert(d.getDate());

Thanks

Sarwar Hasan
  • 1,561
  • 2
  • 17
  • 25