简体   繁体   中英

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 +"," +@m.End_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?

您可以使用此解决方案

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

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.

您可以只使用c#的DateTime.ToLongDateString()并将其传递到javascript中的Date对象的构造函数中,只要您希望日期保持不变即可。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM