2

Actually I'm working on an old existing ASP.NET project. My task is to add an first date of the week output to the calendar week output.

Example:

Example

The data comes from an ASP.NET model.

Actually it works like this:

function SetCalendarWeeks(data) {
    $("#calendarWeek1").text("KW "+data.Week1.Number);
    $("#calendarWeek2").text("KW "+data.Week2.Number);
    $("#calendarWeek3").text("KW "+data.Week3.Number);
    $("#calendarWeek4").text("KW "+data.Week4.Number);
    $("#calendarWeek5").text("KW "+data.Week5.Number);

    if (!data.MonthHas6Weeks) {
        $(".collapsable").hide();
        $(".dummyColumn").show();

        if (data.HideLastInputbox) { 
            $("#planned10").hide();
        } else {
            $("#planned10").show();
        }
    } else {
        $("#calendarWeek6").text("KW " + data.Week6.Number);
        $(".collapsable").show();
        $(".dummyColumn").hide();
        $("#planned10").show();

        if (data.HideLastInputbox) { 
            $("#planned12").hide();
        } else {
            $("#planned12").show();
        }
    }

What I tried is to add this:

document.getElementById("calendarWeek1").innerHTML = "KW "+data.Week1.Number+"&nbsp;<span class='firstDate'>"+data.Week1.FirstDate+"</span>";

But I got this:

Example 2

Can someone help me out?

2
  • What is the type of Week1.FirstDate? Commented Oct 17, 2017 at 7:27
  • public DateTime FirstDate { get; set; } Commented Oct 17, 2017 at 7:37

2 Answers 2

2

You need to convert your date returned by your model to a JavaScript date.

Date returned by your model is in the following format:

/Date(1475272800000)/

I made a function to convert your date; if the parameter short is true the date is converted to the format DD.MM.YYYY:

function ConvertDate(d, short) {
    var regex = /-?\d+/;
    var match = regex.exec(d);
    var date = new Date(parseInt(match[0]))

    if (short) {
        date = ("0" + date.getDate()).slice(-2) + "." + ("0" + (date.getMonth() + 1)).slice(-2) + "." + date.getFullYear();
    }

    return date;
}

So you can use the function like this:

ConvertDate(data.Week1.FirstDate, true)

The full line:

document.getElementById("calendarWeek1").innerHTML = "KW "+data.Week1.Number+"&nbsp;<span class='firstDate'>"+ConvertDate(data.Week1.FirstDate, true)+"</span>";
Sign up to request clarification or add additional context in comments.

1 Comment

After I add this, nothing changed by changing the month :( but now Visual Studio got me this: "Property of global variable 'FirstDate' is possibly never assigned"
0

function ConvertToDate(){
var rawDate = $("#rawDate").val();
var myDate = new Date(Number(rawDate));
$("#myDate").val(myDate);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
input: <input type="text" id="rawDate" value="1482706800000" /> <button onclick="ConvertToDate()">Convert</button>
<br /><br />
output: <input type="text" id="myDate" value="" readonly />

convert your data to date in javascript:

var myDate = new Date(data.Week1.FirstDate);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.