5

I have a problem in converting date in javascript . i try this snippet:

$(document).ready(function () {
            var date = new Date();
            var d = date.getDate();
            var m = date.getMonth();
            var y = date.getFullYear();

            $('#calendar').fullCalendar({
                theme: true,
                header: {left: 'prev,next today',center: 'title',right: 'month,agendaWeek,agendaDay'},
                editable: true,
                events: [
                            {
                    title: 'Birthday Party',
                    start: new Date(y, m, d+1, 19, 0),
                    end: new Date(y, m, d+1, 22, 30),
                    allDay: false
                    }
                        ]

                });
            });

and it works, but if i change it to:

 $(document).ready(function () {
            var date = new Date();
            var d = date.getDate();
            var m = date.getMonth();
            var y = date.getFullYear();

            $('#calendar').fullCalendar({
                theme: true,
                header: {left: 'prev,next today',center: 'title',right: 'month,agendaWeek,agendaDay'},
                editable: true,
                events: [
                         @foreach (var m in Model.Get_List_Tache())
                          {
                        @:{ title: "Tache_description", start: new Date(@m.Begin_date.Year +"," + @m.Begin_date.Month +","+ @m.Begin_date.Day ) , end: new Date( @m.End_date.Year +"," [email protected]_date.Month +"," + @m.End_date.Day ) }
                          }
                        ]

                });
            });

There is a syntaxic error in the format of date.

So what is the reason of this error? How can i fix it?

7
  • 3
    try replace those "," with just , Commented Jul 16, 2013 at 10:14
  • 2
    I don't get the string concatenation going on there. Commented Jul 16, 2013 at 10:30
  • 2
    possible duplicate of Accessing C# variable from Javascript in asp.net mvc application Commented Jul 16, 2013 at 10:30
  • 2
    Why are you using a foreach? I don't think it would like it if you tried to set those properties twice. Commented Jul 16, 2013 at 10:30
  • 2
    Do a 'view source'. I suspect that your loop is outputting invalid JavaScript. Commented Jul 16, 2013 at 10:31

3 Answers 3

8

You can use this solution

start: new Date(@m.Begin_date.ToString("yyyy,MM-1,dd"))

Sign up to request clarification or add additional context in comments.

1 Comment

var date = new Date(@DateTime.ToString("yyyy,MM-1,dd,HH,mm,ss"))
2

I'm still with FosterZ, but you should also get rid of the + as well.

So instead of

start: new Date(@m.Begin_date.Year +"," + @m.Begin_date.Month +","+ @m.Begin_date.Day ) ,

try

start: new Date(@m.Begin_date.Year , @m.Begin_date.Month , @m.Begin_date.Day ) ,

If that still doesn't work, then view the source of the page, and see what is being put into the Javascript there. It's most likely not what you're expecting it to be. If you can't figure it out, add that to your question and we can take a look at it.

1 Comment

Almost! Remember to -1 to the month as months in JavaScript start at 0 and not 1 like C# DateTime
1

You can just use c#'s DateTime.ToLongDateString() and pass it into the constructor of the Date object in javascript as long as you want the date as is without modification.

 start: new Date(@m.Begin_date.ToLongDateString());

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.