gpt4 book ai didi

How to return Datetime correctly in WEB API from Database(如何在WEB API中从数据库中正确返回Datetime)

转载 作者:bug小助手 更新时间:2023-10-22 18:18:49 26 4
gpt4 key购买 nike



I have the following entity which has 2 datetimefields..

我有以下实体,它有2个日期时间字段。。



public class Contactos
{

public int IdContacto { get; set; }


public string PrimerNombre {
get;set;
}

//Code cut for brevity

public DateTime? FechaConsulta { get; set; }

public DateTime? FechaArchivado { get; set; }



}


then on my api I have this

然后在我的api上我有这个



  [ResponseType(typeof(Contactos))]
public List<Contactos> GetContactosDepurar(DateTime fechaInicial)
{
var contactos = ContactosDao.GetContactosDepurados(fechaInicial);
return contactos.ToList();
}


and this

这个



  public static List<Contactos> GetContactosDepurados(DateTime fechaInicial)
{
var fecha = fechaInicial.ToString("yyyy/MM/dd");
string sql = @"SP_ConsultarContactosDepurar";
return Db.Read(sql, ContactosDepurar, new object[]
{
"@fechaInicial", fechaInicial
}).ToList();

}

static readonly Func<IDataReader, Contactos> ContactosDepurar = reader =>
new Contactos
{
IdContacto = reader["idContacto"].AsInt(),
PrimerNombre = reader["primerNombre"].ToString(),
FechaArchivado = reader["fechaArchivado"] is DBNull ? (DateTime?)null : DateTime.ParseExact(reader["fechaArchivado"].ToString(), "dd/mm/yyyy", CultureInfo.CurrentCulture),
FechaConsulta = reader["fechaConsulta"] is DBNull ? (DateTime?)null: DateTime.ParseExact(reader["fechaConsulta"].ToString(), "dd/mm/yyyy", CultureInfo.CurrentCulture)
};


The problem is that when checking the RETURN values on POSTMAN, the date field is being returned as a number:

问题是,在POSTMAN上检查RETURN值时,日期字段将以数字形式返回:



   "FechaConsulta": "/Date(1459832400000-0500)/",

更多回答
优秀答案推荐

Just need to convert the time to your local time using the built in Time Functions. Take your pick from here:

只需要使用内置的时间函数将时间转换为您的本地时间。从这里挑选:



https://msdn.microsoft.com/en-us/library/windows/desktop/ms725473(v=vs.85).aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/ms725473(v=vs.85).aspx



I would pass a Unix/Epoch time stamp for the date. I work with Stripe quite a bit and this is how they pass all their datetimes. I adapted my answer from this post.

我会传递一个Unix/Epoch时间戳作为日期。我经常和Stripe合作,这就是他们打发所有约会时间的方式。我根据这篇文章改写了我的答案。



        var myDate = new DateTime(2017, 5, 5);
int unixTimestamp = (int)(myDate.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;


A quick synapses is to realize that a DateTime is just a number sequence and that is how it is handled internally. When you want to see the value of DateTime you will most likely want to format that into a textual representation of the date.

一个快速的突触是意识到DateTime只是一个数字序列,这就是它在内部的处理方式。当您想查看DateTime的值时,您很可能希望将其格式化为日期的文本表示。



So instead of returning FechaConsulta and FechaArchivado, you should be returning
FechaConsulta.ToString("yyyy/MM/dd") and FechaArchivado.ToString("yyyy/MM/dd")

因此,您不应该返回FechaConsulta和FechaArchivado,而应该返回FechasConsulta.ToString(“yyyy/MM/dd”)和FechasArchivado.ToString(“yyyy/MM/d”)



For a relative quick reading on what DateTime is and how to work with it, please read the answer I posted on a question about how SQL Server stores a DateTime, how the Application works with it, and how to change display formats
Sql saving date with incorrect format

为了相对快速地了解什么是DateTime以及如何使用它,请阅读我发布的关于SQL Server如何存储DateTime、应用程序如何使用它以及如何更改显示格式的问题的答案SQL保存日期格式不正确



[Route("api/[controller]")]
[ApiController]
public class DateTimeController : ControllerBase
{
[HttpGet]
public IActionResult GetDateTime()
{
DateTime CurrentdateTime = DateTime.Now;
return Ok(CurrentdateTime);
}
}

更多回答

If she is returning a Datetime, why on the APÎ response it returns an integer number? her question is how to make it really return a DateTime string, something like 22-02-2017 instead of that integer.

如果她返回的是Datetime,为什么在AP岛响应中会返回一个整数?她的问题是如何让它真正返回DateTime字符串,比如22-02-2017,而不是那个整数。

It's not a matter of fixing the time, it is a matter of displaying it.

这不是固定时间的问题,而是展示时间的问题。

Its displaying a format that is acceptable to be sent across Web APIs. Its a standard format, referred to 'machine time'. its the amount of seconds from jan 1 1970. If the user wants the API to send the time differently, then they will have to reprogram the code for the API. Otherwise, you must convert the time sent to a readable time format.

它显示了一种可以通过Web API发送的格式。它是一种标准格式,被称为“机器时间”。这是1970年1月1日的秒数。如果用户希望API以不同的方式发送时间,那么他们将不得不为API重新编程代码。否则,必须将发送的时间转换为可读的时间格式。

f she is returning a Datetime, why on the APÎ response it returns an integer number? her question is how to make it really return a DateTime string, something like 22-02-2017 instead of that integer.

如果她返回的是Datetime,为什么在AP岛响应中会返回一个整数?她的问题是如何让它真正返回DateTime字符串,比如22-02-2017,而不是那个整数。

The date that they are getting is already an Epoch value with a time offset, what they need is how to read it in a "human" format

他们得到的日期已经是一个带有时间偏移的Epoch值,他们需要的是如何以“人类”格式读取它

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com