Datetime Javascript to Datetime C #

2

I am trying to send an object by means of a POST request, however I have some problems with one of the attributes of that object (datetime).

date = $("#dpkparam").val();             
fecha = new Date(date.replace(/(\d{2})-(\d{2})-(\d{4})/, "$2/$1/$3"));

var perfil = {};
    perfil.idperfil = 100;
    perfil.nombre = "nuevoperfil";
    perfil.LogFechacrea = fecha;

    $.ajax({
        type: 'POST',
        url: pathservicehost + '/perfiles',
        data: JSON.stringify(perfil),
        dataType: 'JSON',
        contentType: 'application/json; charset=utf-8',
        success: function (data, textStatus, res) {
            alert("Perfil Ok...");
        },
        error: function (e) {
            alert('Perfil Falló... ');
        }
    });

The class that I try to save in the database is the following:

public class CPerfil : CGenerico<int>
{
    [DataMember]
    public int idperfil;

    [DataMember]
    public string nombre;

    [DataMember]
    public DateTime LogFechacrea;

}

public void save(CPerfil obj)
{
    using (SEntidades.Entidades ctx = new SEntidades.Entidades())
    {
        perfil objPerfil = new perfil();
        objPerfil.idperfil = obj.idperfil;
        objPerfil.descripcion = obj.nombre;
        objPerfil.log_fechacrea = DateTime.Now;
        //objPerfil.log_fechacrea = obj.LogFechacrea;  error

        ctx.perfil.AddObject(objPerfil);
        ctx.SaveChanges();

        return 1;
    }
}

The error is in the date ... and what I need to know is what is the way to send this attribute.

Thank you.

    
asked by alexander zevallos 13.04.2016 в 23:40
source

2 answers

1
var date = new Date();
var day = date.getDay();        
var month = date.getMonth();    
var year = date.getFullYear();  
var hour = date.getHours();     
var minute = date.getMinutes(); 
var second = date.getSeconds(); 


var time = day + "/" + month + "/" + year + " " + hour + ':' + minute + ':' + second;

DateTime.ParseExact(YourString, "dd/MM/yyyy HH:mm:ss", CultureInfo.InvariantCulture);
    
answered by 14.04.2016 в 19:48
1

Use the library moment.js to format your date, JSON can serialize it on the client and deserialize it on the server

date = $("#dpkparam").val();             
fecha = moment(date).format();

var perfil = {};
perfil.idperfil = 100;
perfil.nombre = "nuevoperfil";
perfil.LogFechacrea = fecha;

$.ajax({
    type: 'POST',
    url: pathservicehost + '/perfiles',
    data: JSON.stringify(perfil),
    dataType: 'JSON',
    contentType: 'application/json; charset=utf-8',
    success: function (data, textStatus, res) {
        alert("Perfil Ok...");
    },
    error: function (e) {
        alert('Perfil Falló... ');
    }
});

Explanation:

moment (date) .format () converts the format of the date to ISO-8601 keeping the UTC information

In the server post:

The date received has the property DateTimeKind = Local.

To save the date on the database server you have to change the DateTimeKind property to DateTimeKind.Utc

objPerfil.log_fechacrea=  objPerfil.log_fechacrea.ToUniversalTime();

In the server GET

Before sending the server date to the client you have to make sure that the DateTimeKind property is Utc

objPerfil.log_fechacrea=  DateTime.SpecifyKind(objPerfil.log_fechacrea, DateTimeKind.Utc);

On the client

In this way JSON understands that you must apply the offset specified in the UTC format and display the local time.

This He helped me a lot

    
answered by 14.04.2016 в 21:26