1

my function returns the selected date of a calendar in this format:

var returnValue = "6.7.2017"; //day.month.year

When i try to use it for a new Date, it does not work:

var weekdays = ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'];
var wkdayname = new Date(returnValue); //Error: returnValue ist NaN
var dayName = weekdays[wkdayname.getDay()];

All I want is just the name of the weekday of this date.

Do you have any suggestions ?

  • 2
    have to ask: did you return the `returnValue`? because `new Date('6.7.2017')` seems to work – Jorg Jul 31 '17 at 07:16
  • This is working fine. Date supports the format you are providing. Can you share your code? – Sumit Jul 31 '17 at 07:17
  • Try `new Date(returnValue.replace(/\./g, '-'))`. If you are open for library, you should use moment.js. Then you can do `moment(returnValue, "MM.DD.YYYY")`. You can also try `new Date(Date.parse(returnValue))` – Rajesh Jul 31 '17 at 07:18
  • your mask date is not valid for javascript "d.m.yyyy" – Álvaro Touzón Jul 31 '17 at 07:19
  • Check the reference to `new Date` to format correctly the constructor [MDN](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date) – mabe02 Jul 31 '17 at 07:19

2 Answers2

-1

The date format also has the day and month switched from the format that Date() recognizes. This function transforms a date string using . and day first notation to the valid format:

function transformDate (date){
 var day_month_year = date.split(".")
    return [day_month_year[1],day_month_year[0],day_month_year[2]].join("/")
} 

var returnValue = "6.7.2017"; //This is a string

var weekdays = ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'];
var wkdayname = new Date(transformDate(returnValue)); 
var dayName = weekdays[wkdayname.getDay()];
alert(dayName)
Nick
  • 3,231
  • 2
  • 28
  • 50
IanS5
  • 94
  • 3
  • this solution works absolutely fine in Firefox! Thank you! –  Jul 31 '17 at 07:28
  • There are many, many duplicates. You should avoid the parser completely, using the built-in parser is fraught. Parsing a string to format another string that is parsed by the built-in parser doesn't make a lot of sense. Just parse the string and give the parts to the Date constructor. – RobG Jul 31 '17 at 22:38
  • Yeah... i dont know how^^ –  Aug 01 '17 at 11:52
-1

You can validate a date string using the Date.parse() API.

But mind you, this API's validation is a bit loose as it accepts both ISO 8601 or RFC 2822 date formats. If it is a valid date then the API returns you the epoch time which can then be used for creating the Javascript Date object. Otherwise it returns NaN.

As for getting the day - you can use the .getDay() API.

Example:

if (Date.parse("aaabbcc")) {
    console.log("aaabbcc is Valid");
}
else {
    console.log("aaabbcc is invalid");
}

if (Date.parse("6.7.2017")) {
    var date = new Date(Date.parse("6.7.2017"));
    console.log("6.7.2017 is Valid");
    console.log("The day of the week is => " + date.getDay());
}
else {
    console.log("6.7.2017 is invalid");
}

References: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse

Samuel Toh
  • 18,006
  • 3
  • 24
  • 39
  • See the advice in your second reference: "*It is not recommended to use Date.parse … There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed*". – RobG Jul 31 '17 at 22:40