user2443476
user2443476

Reputation: 1975

convert .net date format to javascript format

I am developping an ASP.NET MVC application where I use an infragistics grid to display data, I have a date format column where I can display several formats. In my .NET server side, I store format like this :

{0:d}
{0:D}
{0:G}
{0:s}

My infragistics JavaScript grid can only understand this kind of format :

dd/mm/yyyy
dddd d MMMM yyyy
...
...

I would like to know how to convert the .NET format to understable JavaScript format without doing tricky string manipulation. There is no native converter to pass from this {0:d} to that dd/mm/yyyy in c#.

Thanks in advance for your help

Upvotes: 2

Views: 1457

Answers (2)

Ibrahim Abdelkareem
Ibrahim Abdelkareem

Reputation: 973

Here you go ^^ let's create custom converter

 public class DateTimeConverter : JavaScriptConverter
    {
      public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
      {
        throw new NotImplementedException();
      }


  public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
  {
    return new Dictionary<string, object> { { "", ((DateTime)obj).ToString("dd/MM/yyyy") } };
  }


  public override IEnumerable<Type> SupportedTypes { get { return [] { typeof(DateTime) }; } }


}

Then let's create custom JsonResult class.

public class CustomJsonResult : JsonResult
{
  public override void ExecuteResult(ControllerContext context)
  {
      if (context == null)
      {
          throw new ArgumentNullException("context");
      }
      HttpResponseBase response = context.HttpContext.Response;
      if (!string.IsNullOrEmpty(this.ContentType))
      {
        response.ContentType = this.ContentType;
      }
      else
      {
        response.ContentType = "application/json";
      }
      if (this.ContentEncoding != null)
      {
        response.ContentEncoding = this.ContentEncoding;
      }
      if (this.Data != null)
      {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        serializer.RegisterConverters(new [] { new DateTimeConverter () });
        response.Write(serializer.Serialize(this.Data));
      }
  }
}

In your Base Controller override Json Method to return CustomJsonResult

public class BaseController : Controller
{
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) {
    return new CustomJsonResult{
        Data = data,
        ContentType = contentType,
        ContentEncoding = contentEncoding
    };
}
}

Then let's return our custom Json Format

public class AppController : BaseController
{
public ActionResult MyAction()
{
  //Assuming there's a variable called data
return Json(data,JsonBehavior.AllowGet);
}
}

Upvotes: 0

Richard Schneider
Richard Schneider

Reputation: 35477

Use ToString to format the DateTime value.

var s = myData.type.ToString("dd/MM/yyyy");

See Custom Date and Time Formats for more details.

Upvotes: 2

Related Questions