User face problem format the date string in a particular culture in JavaScript. So, in this blog, I will tell you how to format
the date string in JavaScript.
In page load event of page
Defined the JavaScript variable dateFormat.
Variable dateFormat contain the culture (In which format user want to show date)
-
- Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "DateFormat", "var dateFormat ='" + Session["DateFormat"] + "'", true);
-
-
- function ParseDate(dateString) {
-
-
- dateParts = dateString.split("/");
- if (dateParts.length != 3)
- return undefined;
- else {
- var ReturnDate = new Date();
-
- if (dateFormat == "d/M/yyyy") {
- ReturnDate.setMonth(dateParts[1] - 1, dateParts[0]);
- } else {
- ReturnDate.setMonth(dateParts[0] - 1, dateParts[1]);
- }
-
- ReturnDate.setFullYear(y2k(dateParts[2]));
- ReturnDate.setHours(0);
- ReturnDate.setMinutes(0);
- ReturnDate.setSeconds(0);
- ReturnDate.setMilliseconds(0);
- return ReturnDate;
- }
- }
-
-
-
- function y2k(yearString) {
-
- if (yearString.length == 4) {
- return yearString;
- } else if (yearString.length == 2) {
- var year = Number(yearString);
- if (year < 50) {
- year = year + 2000;
- } else {
- year = year + 1900;
- }
- return year;
- } else {
- return 0;
- }
- }