2

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

2 Answers 2

2

Use ToString to format the DateTime value.

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

See Custom Date and Time Formats for more details.

Sign up to request clarification or add additional context in comments.

3 Comments

See also: Custom Date and Time Format Strings. And note that the format string is case sensitive. MM is month and mmis minutes.
This is not my problem, I don't want to specify a format, I already have the format but it's in a .net way.
A .net DateTime has no format. It's basically just an Int64 value (see DateTime under "Remarks" for details). Only its representation (i.e. ToString) has a format.
0

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);
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.