locked
Change date format /Date(1337878800000+0700)/ to regular date format, example dd/mm/yyyy hh:mm:ss RRS feed

  • Question

  • User1751268424 posted

    I make a ToDoItem in Single Page Application, using model Activity.cs instead of ToDoItem.cs.

    Here my Activity.cs

    namespace ToDoActivity.Models
    {
        public class Activity
        {
            public int Id { get; set; }
            [Required]
            [StringLength(50)]
            public string Name { get; set; }
            public string Description { get; set; }
            public int ParentId { get; set; }
            public int PersonInCharge { get; set; }
            public System.DateTime DateStart { get; set; }
            public Nullable<System.DateTime> DateEnd { get; set; }
            public System.DateTime ModifiedDate { get; set; }
    
            [Display(AutoGenerateField = false)]
            public System.Guid? rowGuid { get; set; }
        }
    }

    On _Grid.cshtml, 

    <h2>Activities (<span data-bind="text: activities().length"></span>)</h2>
    
    <table>
        <thead>
            <tr>
                 <th>Id</th>
                 <th>Name</th>
                 <th>Description</th>
                 <th>ParentId</th>
                 <th>PersonInCharge</th>
                 <th>DateStart</th>
                 <th>DateEnd</th>
                 <th>ModifiedDate</th>
                 <th>rowGuid</th>
            </tr>
        </thead>
        <tbody data-bind="foreach: activities">
            <tr data-bind="css: { added: IsAdded, updated: IsUpdated, deleted: IsDeleted, error: EntityError }">
                <td data-bind="text: Id"></td>
                <td data-bind="text: Name"></td>
                <td data-bind="text: Description"></td>
                <td data-bind="text: ParentId"></td>
                <td data-bind="text: PersonInCharge"></td>
                <td data-bind="text: DateStart"></td>
                <td data-bind="text: DateEnd"></td>
                <td data-bind="text: ModifiedDate"></td>
                <td data-bind="text: rowGuid"></td>
                <td><a href="#" data-bind="click: $parent.openEditor">Edit</a></td>
            </tr>
        </tbody>
    </table>
    
    <p><a href="#" data-bind="click: createActivity">Create new</a></p>
    <button data-bind="click: saveAll">Save all</button>
    <button data-bind="click: revertAll">Revert all</button>

    Result in browser:

    /Date(1337878800000+0700)/

    I can not type dd-mmm-yyyy during create a new activity or edit an activity. 

    I need to change date format to regular date format, during create a new acitivity and edit an activity. If possible using DatePicker for editing, and display format dd/mm/yyyy or other regular format. 

    Please help

    Thursday, May 24, 2012 10:51 PM

Answers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 25, 2012 9:33 AM
  • User1751268424 posted

    Hi all,

    Now I have a progress. I can make date from /Date(1337878800000+0700)/ to 2012-05-25T00:00:00, 

    But I still looking for the good format remove T00:00:00 on short date format.

    The solution based on http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx.

    at part of comment of Raffi Krikorian @raffi:

    QUOTE

    We're going to do this by default in ASP.NET Web API when it releases. (We aren't doing this now in Beta) You can see how to swap out the serializer to JSON.NET on Henrik's blog. You can also check out the Thinktecture.Web.Http convenience methods that bundles some useful methods for ASP.NET Web API.

    Today with the Beta, I just need to update my global.asax and swap out the JSON Formatter like this (see Henrik's blog for the full code):

    1
    2
    3
    4
    // Create Json.Net formatter serializing DateTime using the ISO 8601 format
    JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
    serializerSettings.Converters.Add(new IsoDateTimeConverter());
    GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings);

    When we ship, none of this will be needed as it should be the default which is much nicer. JSON.NET will be the default serializer AND Web API will use ISO 8601 on the wire as the default date format for JSON APIs.

    QUOTE

    I add the following code to Web.Config after following the the above reference:

                JsonSerializerSettings serializerSettings = new JsonSerializerSettings();                          //  using Newtonsoft.Json;
                serializerSettings.Converters.Add(new IsoDateTimeConverter());                                     //  using Newtonsoft.Json.Converters;
                GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings);        //  using Thinktecture.Web.Http.Formatters;



    I am going to get back here after I can solved the short date format

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 26, 2012 2:01 AM

All replies

  • User-836030956 posted

    Hope it will help

    i have done small function in my project for  clinet side to get date

    var value="/Date(1337878800000+0700)/";
    if (value.substring(0, 6) == "/Date(")
     {
       var dt = new Date(parseInt(value.substring(6, value.length - 2)));
       var dtString = (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
     }



    Friday, May 25, 2012 6:00 AM
  • User1751268424 posted

    Hi Bhavik,

    Thank's for reply. But this in SPA of MVC, I've looked at knockoutjs.com, but it never has any kind of that date format. I really don't know that kind of date format is that. I got that date when I update regular date on my SQL database directly, and render with the view I posted, the date format on the display become like that.

    On database: 24/05/2012 00:00:00, on my view: /Date(1337878800000+0700)/

    I do not familiar with that date. The problem is I have to enter the date during new entry or edit. 

    It is not practice to enter or edit this kind of date format by user.

    Again thanks for trying to help.

    Friday, May 25, 2012 8:10 AM
  • User208921113 posted

    I have the same problem,

    here is just an idea, in the MVVM ViewModel:

      self.TimePropertyOriginal = ko.observable(); // TimePropertyOriginal == "/Date(1337878800000+0700)/"

    self.TimePropertyComputed= ko.computed({  // TimePropertyComputed == "24/05/2012 00:00:00"
        read: function () { //TODO: convert self.TimePropertyOriginal to "24/05/2012 00:00:00", and return it. }, 
        write: function(){ 
                                 //TODO: convert "24/05/2012 00:00:00" from the timepicker to "/Date(1337878800000+0700)/"
                                 //and set it to self.TimePropertyOriginal 
                               }
    });
    btw:in javascript, new Date(1337878800000).getTime() == 1337878800000 is true.
    Friday, May 25, 2012 8:40 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, May 25, 2012 9:33 AM
  • User28563431 posted

    Please change dateformat to your required format and then convert it to string.You can add one more property 

    in Activity class like 

    public string DateStartString { get; set; }
    Use above property in client end 
    Friday, May 25, 2012 9:40 AM
  • User1751268424 posted

    Hi,

    Thank you all for all replies, I have not success yet, I'm keeping trying BrockAllen has, also not success yet, I'm sure this is now about my knowledge still in a learning phase, I need more concentration to use this site http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx.

    Again thank you all, I'll come here again success or not.

     

    Friday, May 25, 2012 6:48 PM
  • User1751268424 posted

    Hi all,

    Now I have a progress. I can make date from /Date(1337878800000+0700)/ to 2012-05-25T00:00:00, 

    But I still looking for the good format remove T00:00:00 on short date format.

    The solution based on http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx.

    at part of comment of Raffi Krikorian @raffi:

    QUOTE

    We're going to do this by default in ASP.NET Web API when it releases. (We aren't doing this now in Beta) You can see how to swap out the serializer to JSON.NET on Henrik's blog. You can also check out the Thinktecture.Web.Http convenience methods that bundles some useful methods for ASP.NET Web API.

    Today with the Beta, I just need to update my global.asax and swap out the JSON Formatter like this (see Henrik's blog for the full code):

    1
    2
    3
    4
    // Create Json.Net formatter serializing DateTime using the ISO 8601 format
    JsonSerializerSettings serializerSettings = new JsonSerializerSettings();
    serializerSettings.Converters.Add(new IsoDateTimeConverter());
    GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings);

    When we ship, none of this will be needed as it should be the default which is much nicer. JSON.NET will be the default serializer AND Web API will use ISO 8601 on the wire as the default date format for JSON APIs.

    QUOTE

    I add the following code to Web.Config after following the the above reference:

                JsonSerializerSettings serializerSettings = new JsonSerializerSettings();                          //  using Newtonsoft.Json;
                serializerSettings.Converters.Add(new IsoDateTimeConverter());                                     //  using Newtonsoft.Json.Converters;
                GlobalConfiguration.Configuration.Formatters[0] = new JsonNetFormatter(serializerSettings);        //  using Thinktecture.Web.Http.Formatters;



    I am going to get back here after I can solved the short date format

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 26, 2012 2:01 AM
  • User208921113 posted

    Still, you may run into the problem of "This DataController does not support operation insert for entity jobject" when trying to do a form POST(eg.: create new Activity) using the JsonNetFormatter,

    Good luck.

    Saturday, May 26, 2012 2:12 AM