Dipesh Parmar
Dipesh Parmar

Reputation: 27382

get proper date in jQuery

I am working on one project and i am using FullCalendar and jQuery dialog.

I am using jQuery dialog because user select any time-slot they will be see one dialog box and inside of that dialog box i will pass start and end time.

But problem is that fullCalendar pass start argument as date object which is Date {Tue Apr 02 2013 00:00:00 GMT+0530 (India Standard Time)} now problem is that how can just display 02-04-2013 00:00:00 i do not want to use more plugin as its already have too much js included.

What i have?

jQuery Dialog code

$( "#dialog-form" ).dialog({
    autoOpen: false,
    modal: true,
    buttons:
    {
        "Create an event": function()
        {
            var bValid = true;
            if ( bValid )
            {
                //adding event to calendar code
                $( this ).dialog( "close" );
            }
        },
        Cancel: function()
        {
            $( this ).dialog( "close" );
        }
    },
    close: function()
    {
        allFields.val( "" ).removeClass( "ui-state-error" );
    }
});

FullCalendar

var calendar = $('#calendar').fullCalendar(
{
    header:
    {
        left: 'prev,next today',
        center: 'title',
        right: 'month,agendaWeek,agendaDay'
    },
    selectable: true,
    selectHelper: true,
    select: function(start, end, allDay)
    {
        console.log(start);
        $("#dialog-form input#start_time").val(start);
        $("#dialog-form input#end_time").val(end);
        $( "#dialog-form" ).dialog( "open" );
        calendar.fullCalendar('unselect');
    },

I pasted only half code here because its too long and its not necessary to add it here.

Thanks

Upvotes: 1

Views: 269

Answers (3)

Ketan Modi
Ketan Modi

Reputation: 1800

$.fullCalendar.formatDate( start, 'dd-MM-yyyy HH:mm:ss' );

Upvotes: 3

Jaya Mayu
Jaya Mayu

Reputation: 17257

update

Full calendar has a built in conversion utility

$.fullCalendar.formatDate( date, formatString [, options ] )

Old answer

start is a date object so you can do something as below

var curDate = start.getDate();
var curMonth = (parseInt(start.getMonth())+1);
var curYear = start.getFullYear();

alert(curDate+'-'+curMonth+'-'+curYear);

here is a Live fiddle example.

Upvotes: 0

Dipesh Parmar
Dipesh Parmar

Reputation: 27382

Well Thanx guys for looking and commenting on question but i found it out.

var startDate = start.getFullYear() + "-" + getOtherFiled(start.getMonth()) + "-" + getOtherFiled(start.getDate()) + " " +  getOtherFiled(start.getHours()) + ":" + getOtherFiled(start.getMinutes()) + ":" + getOtherFiled(start.getSeconds());

function getOtherFiled(obj)
{
    if (obj<10)
       return "0"+obj;
    return obj;
}

above code successfully output '2013-03-03 02:30:00' and so on.

Upvotes: 1

Related Questions