Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Datetime C# type to Date javascript in asp.net mvc razor application

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?

like image 572
Lamloumi Afif Avatar asked Jul 16 '13 10:07

Lamloumi Afif


People also ask

What does DateTime ParseExact do?

The DateTime. ParseExact(String, String, IFormatProvider) method parses the string representation of a date, which must be in the format defined by the format parameter.

How do I convert a string to a date in C #?

You can use the methods like Convert. ToDateTime(String), DateTime. Parse() and DateTime. ParseExact() methods for converting a string-based date to a System.

What is the difference between DateTime and DateTimeOffset?

DateTimeOffset contains information about the timezone you are dealing with, which makes all the difference in THE WORLD! The only difference is that it stores only the UTC offset for the particular instant in time that a DateTime represents.


2 Answers

You can use this solution

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

like image 175
Vasil Galinovsky Avatar answered Sep 30 '22 09:09

Vasil Galinovsky


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.

like image 43
krillgar Avatar answered Sep 30 '22 11:09

krillgar