4
I have a return from json
that comes so from SqlServer
:
/Date(1386295200000)/
How to convert to date format dd/mm/yyyy
?
4
I have a return from json
that comes so from SqlServer
:
/Date(1386295200000)/
How to convert to date format dd/mm/yyyy
?
3
I believe you are making a Json call and have a date field, just like the Json below:
{
"rows":[
{
"UsuarioConviteID":1,
"Email":"[email protected]",
"Controle":"Aceito",
"DtControle":"\/Date(1518540878497)\/",
"DtCad":"\/Date(1518526866667)\/",
"Status":"A",
"Fixo":"Não"
}
],
"total":1
}
For you to convert data using Jquery can use the function:
function formataData(value) {
if (value == null)
return null;
var dataTexto = value.replace('/', '').replace('/', '').replace('Date', '').replace('(', '').replace(')', '');
var date = new Date(parseInt(dataTexto));
return pad(2, date.getDate().toString(), '0') + "/" + pad(2, (date.getMonth() + 1).toString(), '0') + "/" + pad(4, date.getFullYear().toString(), '0');
};
Where do you use it to display on a bootgrid for example
@gabrielfalieri just a moment to see how bootgrid works
@gabrielfalieri if you look at the bootgrid documentation at http://www.jquery-bootgrid.com/Examples#data you have a function formatters: {
 "link": function(column, row)
 {
 return "<a href=\"#\">" + column.id + ": " + row.id + "</a>";
 }
there you could put the function
2
The library Momentjs
already supports ASP.NET JSON Date
, see the documentation.
Code example:
moment("/Date(1386295200000)/").format("DD/MM/YYYY"); // 06/12/2013
moment("/Date(1198908717056-0700)/"); // Sat Dec 29 2007 04:11:57 GMT-0200
moment("/Date(1198908717056-0700)/").format("DD/MM/YYYY"); // 29/12/2007
See the result:
$(function() {
var result = moment('/Date(1386295200000)/').format("DD/MM/YYYY");
alert(result);
result = moment("/Date(1198908717056-0700)/");
alert(result);
result = moment("/Date(1198908717056-0700)/").format("DD/MM/YYYY");
alert(result);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.1/moment.min.js"></script>
Or if you prefer in Jsfiddle.
2
The best option in this case is inherit the class JsonResult
and change the date format setting as follows:
Class JsonResultDateFormat
:
public class JsonResultDateFormat : JsonResult
{
public JsonResultDateFormat(object data,
JsonRequestBehavior jsonRequestBehavior = JsonRequestBehavior.AllowGet)
{
Data = data;
JsonRequestBehavior = jsonRequestBehavior;
}
public override void ExecuteResult(ControllerContext context)
{
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "application/json";
if (ContentEncoding != null) response.ContentEncoding = ContentEncoding;
JsonTextWriter writer = new JsonTextWriter(response.Output) {
Formatting = Formatting.Indented
};
JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings()
{
DateFormatString = "dd/MM/yyyy" // aqui o formato da data
});
serializer.Serialize(writer, Data);
writer.Flush();
}
public static JsonResultDateFormat Create(object data,
JsonRequestBehavior jsonRequestBehavior = JsonRequestBehavior.AllowGet)
=> new JsonResultDateFormat(data, jsonRequestBehavior);
}
and the method of Controller
:
public JsonResult Test()
{
using (DatabasecoreContext c = new DatabasecoreContext())
{
var data = c.Notice.ToList();
return JsonResultDateFormat.Create(data);
}
}
your exit is:
[
{
"Id": 1,
"CreditId": 1,
"Title": "Esporte",
"DateCreated": "01/02/2018"
},
{
"Id": 2,
"CreditId": 2,
"Title": "Esporte",
"DateCreated": "01/02/2018"
}
]
Reference: Dealing with JSON Dates in ASP.NET MVC
I’m going to test your hiccup here now
Perfect @Virgilio-novic
1
On the server side the date is probably correct. Usually this happens in the conversion of DateTime
for Json
(and we usually treat the return in JavaScript
, I don’t know if that’s your case, the question doesn’t address that).
If you want to make the adjustment only on the front-end side via javascript, create a function like this:
function ConverterJsonDateToJavascriptDate(data)
{
var retorno = new Date(parseInt(jsonDate.substr(6)));
}
Use like this:
var jsonDate = "/Date(1386295200000)/";
var resultado = ConverterJsonDateToJavascriptDate(jsonDate);
1
I recommend using the framework Momentjs to do treatment dates. Here is an example from the documentation of Bootgrid
$("#grid").bootgrid({
converters: {
datetime: {
from: function (value) { return moment(value); },
to: function (value) { return value.format("lll"); }
}
}
});
Another alternative is to control date output using Jsonproperty Annotation :
using System;
using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace WebApiDemo.Controllers
{
public class ValuesController : ApiController
{
// GET api/values
public IHttpActionResult Get()
{
var demo = new DemoViewModel { Data = DateTime.UtcNow };
return Ok(demo);
}
}
public class DemoViewModel
{
[JsonProperty(ItemConverterType = typeof(IsoDateTimeConverter))]
public DateTime Data { get; set; }
}
}
Browser other questions tagged c# sql asp.net-mvc json entity-framework
You are not signed in. Login or sign up in order to post.
Have you tried any way ? It would be conversion from Unix data to
dd/mm/yyyy
correct !?– rbz
no, because I had never seen this return in life... nor knew it was Unix dataHue, yes it would be
– gabrielfalieri
It comes dynamically from Entity.
– gabrielfalieri
If you are using which backend (programming language)?
– novic
I’m using c# as a backend
– gabrielfalieri
You want to show this on an HTML page and it’s coming from Json?
– Tiedt Tech
You are using Entity Framework with aspnet mvc?
– novic
That, you’re both right, my dear
– gabrielfalieri
Is sql server even @gabrielfalieri ?
– rbz
Yes, sql server
– gabrielfalieri
There you are returning to your Front-End and do not have the format, maybe a simple conversion in the controller itself solves, or even a Viewmodel for this also solve if you have the controller ai?
– novic
Siim, I have the controller
– gabrielfalieri
Inside the Asqueryable there is something I can do that can make this conversion?
– gabrielfalieri
and the Entity Framework version?
– novic