Click here to Skip to main content
14,330,776 members
Rate this:
Please Sign up or sign in to vote.
See more:
<script>
$(function () {
$.ajax({
type: "GET",
url: "/Test/GetMerchant"
}).done(function (data) {
 
        //$("#leftMenu").hide();

        var MyDateField = function (config) {
            jsGrid.Field.call(this, config);
        };
 
        MyDateField.prototype = new jsGrid.Field({
            sorter: function (date1, date2) {
                return new Date(date1) - new Date(date2);
            },
 
            itemTemplate: function (value) {
                debugger
                if (value == "")
                    return "";
                else {
                    var date = new Date(value).toDateString("MM/dd/yyyy")
                    //return new Date(value).toDateString();
                    return value;
                }
            },
 
            insertTemplate: function (value) {
                debugger
                var RDAte = value;   //.toISOString("MM/dd/yyyy")
                var newdate = RDAte.toLocaleDateString('en-US')
 
                var d = new Date(newdate),
                month = '' + (d.getMonth() + 1),
                day = '' + d.getDate(),
                year = d.getFullYear();
 
                if (month.length &lt; 2) month = '0' + month;
                if (day.length &lt; 2) day = '0' + day;
 
                var df = [month, day, year].join('/');
                return this._insertPicker = $("&lt;input&gt;").datepicker({ defaultDate: new Date() });
            },
 
            editTemplate: function (value) {
                debugger
                var RDAte = value;   //.toISOString("MM/dd/yyyy")
                var newdate = RDAte.toLocaleDateString('en-US')
 
                var d = new Date(newdate),
                month = '' + (d.getMonth() + 1),
                day = '' + d.getDate(),
                year = d.getFullYear();
 
                if (month.length &lt; 2) month = '0' + month;
                if (day.length &lt; 2) day = '0' + day;
 
                var df = [month, day, year].join('/');
 
                return this._editPicker = $("&lt;input&gt;").datepicker().datepicker("setDate", new Date(df));
            },
 
            insertValue: function () {
                debugger
                if (this._insertPicker.datepicker("getDate") != null)
                    return this._insertPicker.datepicker("getDate");         //.toISOString("MM/dd/yyyy")
                else
                    return this._insertPicker.datepicker("getDate");
            },
 
            editValue: function () {
                debugger
                if (this._editPicker.datepicker("getDate") != null) {
                    //var RDAte = this._editPicker.datepicker("getDate");   //.toISOString("MM/dd/yyyy")
                    //var newdate = RDAte.toLocaleDateString('en-US')

                    //var d = new Date(newdate),
                    //month = '' + (d.getMonth() + 1),
                    //day = '' + d.getDate(),
                    //year = d.getFullYear();

                    //if (month.length &lt; 2) month = '0' + month;
                    //if (day.length &lt; 2) day = '0' + day;

                    ////var df = [month, day, year].join('/');
                    //return [month, day, year].join('/');
                    return this._editPicker.datepicker("getDate");
                }
                    //return this._editPicker.datepicker("getDate").toISOString();
                else
 
                    //var RDAte = this._editPicker.datepicker("getDate");   //.toISOString("MM/dd/yyyy")
                    //var newdate = RDAte.toLocaleDateString('en-US')

                    //var d = new Date(newdate),
                    //month = '' + (d.getMonth() + 1),
                    //day = '' + d.getDate(),
                    //year = d.getFullYear();

                    //if (month.length &lt; 2) month = '0' + month;
                    //if (day.length &lt; 2) day = '0' + day;

                    ////var df = [month, day, year].join('/');
                    //return [month, day, year].join('/');

                    //return this._editPicker = df;
                    return this._editPicker.datepicker("getDate");
            }
        });
 
        jsGrid.fields.myDateField = MyDateField;
 
        $("#jsGrid").jsGrid({
            height: "50%",
            width: "100%",
            filtering: true,
            editing: true,
            inserting: true,
            sorting: true,
            paging: true,
            autoload: true,
            pageSize: 12,
            pageButtonCount: 5,
 
            //pageLoading: true,

            deleteConfirm: "Do you really want to delete the merchandise?",
            controller: db,
            fields: [
                { type: "control" },
                {
                    name: "Source", type: "text", width: 120, title: "Vendor"
                },
                {
                    name: "Description", type: "text", width: 210,
                    validate: { message: "Description is required!", validator: function (value) { return value != ""; } }
                },
                {
                    name: "ModelNumber", type: "text", width: 120, title: "Model#/Item"
                },
                { name: "SKU", type: "text", width: 90 },
                { name: "SKU2", type: "text", width: 90 },
                { name: "Comments", type: "text", width: 200 },
                { name: "strReceiveDate", type: "myDateField", width: 80, align: "center", title: "Received" },
                { name: "Location", type: "select", items: data.loc, valueField: "LocationID", textField: "Description", width: 100 },
                { name: "Barcode", width: 80 },
                { name: "BarcodePrinted", type: "checkbox", title: "Barcode Printed", sorting: false },
                { name: "strLastUpdatedDate", type: "myDateField", width: 80, title: "Last Updated" },
                { name: "DamageCode", type: "select", items: data.dam, valueField: "CodeID", textField: "CodeValue", title: "Damage" },
                { name: "strCreatedDate", type: "myDateField", width: 80, title: "Created Date" },
                { name: "strShipDate", type: "myDateField", myCustomProperty: "bar", width: 80, title: "Ship Date" },
                { name: "strConsumeDate", type: "myDateField", myCustomProperty: "bar", width: 80, title: "Consume Date" },
                { name: "PendingShipment", type: "checkbox", title: "Pending", sorting: false, width: 60 },
                { name: "Donated", type: "checkbox", title: "Is Donated", sorting: false, width: 60 },
                { name: "ReturnRequested", type: "checkbox", title: "Return Requested", sorting: false },
                { name: "ReturnTo", type: "text", width: 150, title: "Return To" },
                { name: "Quantity", type: "number", width: 50, title: "Qty" },
                { name: "GroupName", type: "text", width: 150, title: "Group Name" },
                { name: "CustomerID", width: 100, title: "Customer ID" },
            ],
        });
    });
});

</script>


What I have tried:

i am trying this code but not work

//var newdate = RDAte.toLocaleDateString('en-US')

//var d = new Date(newdate),
//month = '' + (d.getMonth() + 1),
//day = '' + d.getDate(),
//year = d.getFullYear();

//if (month.length < 2) month = '0' + month;
//if (day.length < 2) day = '0' + day;

//var df = [month, day, year].join('/');


//return this._editPicker = df;

this is my full code and when i edit #jsGrid then date save by default
("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)") format but i need 07/06/2016 this format
Posted
Updated 5-Jul-16 19:37pm
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

Try this code:-

function convert(str) {
var mnths = {
Jan:"01", Feb:"02", Mar:"03", Apr:"04", May:"05", Jun:"06",
Jul:"07", Aug:"08", Sep:"09", Oct:"10", Nov:"11", Dec:"12"
},
date = str.split(" ");

return [ mnths[date[1]], date[2], date[3] ].join("/");
}

convert("Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)");
   
v2
Comments
mrprakash 5-Jul-16 9:54am
   
but i need MM/dd/yyyy this format but by default this (Thu Jun 09 2011 00:00:00 GMT+0530 (India Standard Time)) format show i dont need this format
Rate this:
Please Sign up or sign in to vote.

Solution 2

try this

function GetDateFormat(date) {
           var month = (date.getMonth() + 1).toString();
           month = month.length > 1 ? month : '0' + month;
           var day = date.getDate().toString();
           day = day.length > 1 ? day : '0' + day;
           return month + '/' + day + '/' + date.getFullYear();
       }

       var dateObj = new Date();
       var dateFormat = GetDateFormat(dateObj); // 07/05/2016


Demo:Javascript Date Format - JSFiddle[^]
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 4

Try this one

function formatDate(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
var dd = (date.getDate() < 10 ? '0' : '') + date.getDate();
var MM = ((date.getMonth() + 1) < 10 ? '0' : '') + (date.getMonth() + 1);
var yyyy = date.getFullYear();
return (dd + "/" + MM + "/" + yyyy);
}

This will give outout as "dd/mm/yyyy"
   
Comments
OriginalGriff 10-Oct-19 3:25am
   
While I applaud your urge to help people, it's a good idea to stick to new questions, rather than 3 year old ones. After that amount of time, it's unlikely that the original poster is at all interested in the problem any more!
Answering old questions can be seen as rep-point hunting, which is a form of site abuse. The more trigger happy amongst us will start the process of banning you from the site if you aren't careful. Stick to new questions and you'll be fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100