Web API DateTime not being parsed - asp.net-web-api

I am receiving POST data from a vendor's web service in my web api. One of the values being sent is a data and time - sample: StartTime=2014-10-20+15%3A30%3A54. I understand that %3A is an escape for :, which would have a date in the following format: 2014-10-20+15:30:54.
The problem is that the StartTime value is not being parsed properly by the web api, and all the StartDate properties on my model are being set to 1/1/0001 12:00:00 AM.
Can anyone point me in the right direction here? I can't seem to find any info on what format this date is in to see if there is a way to have the web api framework parse it correctly.

Try this
Request Body:
{"StartTime":"2014-10-20+15:30:54"}
Parse:
CultureInfo provider = CultureInfo.InvariantCulture;
string format = "yyyy-MM-dd+HH:mm:ss";
DateTime dt = DateTime.ParseExact(request.StartTime, format, provider);

Related

Spring Boot JPA TimeZone

I am having some troubles whit an endpoint that returns an object that contains a date (this date comes from a web service response).
So, right now these are the steps
I read the response from the webservice, and i set it on the "TicketAcceso" object.
//example
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
System.out.println("Read from the xml response = "+dateLineXmlResponse);
//OUTPUT = "Read from the xml response = 2019-09-27T01:56:14.467-03:00"
Date expDate = new Date(dateFormat.parse(dateLineXmlResponse).getTime());
ticketAcceso.setFechaVencimiento(expDate);
Save the object (actually from a bigger class, "Session", that contains one "TicketAcceso"), this is working, the date is now saved in mysql.
Then i have a get endpoint that returns a TicketAcceso, the thing is:
. The Date is correct in the database (OK value, without timezone info).
. If i get the date from the object just before returning from the endpoint, the value is ok (it uses the local timezone)
System.out.println("Getting the date from the object before returning =
"+ticketAccesoNuevo.getFechaVencimiento());
//OUTPUT = "Getting the date from the object before returning = Fri Sep 27 01:56:14 ART 2019"
//returns the TicketAcceso
BUT
The endpoint returns the date in UTC timezone
Actual MySQL value =
Endpoint return value =
Is there a way for returning the date in the local TimeZone? I've already added these lines:
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example?
useLegacyDatetimeCode=false
spring.jpa.properties.hibernate.jdbc.time_zone=America/Argentina/Buenos_Aires</code>

Why can't I bind dates in query string with GET parameters?

I am using ASP.NET Core 3.1. I am trying to bind dates in a query string to a PageModel using GET. Here is my query string:
?id=15+startDate=1900-01-01+endDate=1900-01-01
Here is the signature of my OnGet:
public void OnGet(int id, DateTime startDate, DateTime endDate)
The values are always 0, 1/1/0001 00:00:00, 1/1/0001 00:00:00 respectively.
If I remove the dates and just pass the id, it works fine. Why do the dates in the query string break everything including the id? I have also tried changing the data types from DateTime to string and it still breaks with the string parameters always being null.
The separator for parameters in a query string is &, not + as your example has. Corrected:
?id=15&startDate=1900-01-01&endDate=1900-01-01
An arguably better approach with datetime parameters is to accept them as strings, then use DateTime.TryParse to parse with control over expected format. That way you'll always have the opportunity to inspect the string value provided and decide if you want to convert it to a DateTime, and how.

Elastic search update-by-query how work with date field (parse from string?)

I'm new in elastic and I need work with date in update-by-query script.
I have docs with field expire which is mapped as date.
I try in my script in update-by-query:
SimpleDateFormat expire = new SimpleDateFormat(\"yyyy-MM-dd'T'HH:mm:ss Z\", Locale.getDefault());
long expireTime = expire.parse(ctx._source.expire).getTime();
But there is error: Unparseable date: "2017-07-21T11:19:42+02:00"
Is there a different way, how got date object or UNIX timestamp?
Or is there a way how got mapped formats instead of strings in update-by-query?
Thank you.
There's some documentation on working with dates in scripts. Just do this and it will work:
long expireTime = ZonedDateTime.parse(ctx._source.expire).toInstant().toEpochMilli()

Convert unix timestamp to normal Date in Json

I am using laravel 5.0. I am getting data from controller as json. I am having value like this.
{"timstamp_date":"1434360957"},
I need to convert this unix timestamp value as Normal Date Like (15-06-2015) or (15-March-2015).
I have used Date(timstamp_date) but it is showing current time only. Not my timstamp date
You could use:
date("d-m-Y H:i:s", 1434360957);
EDIT
You could try;
var dateTime = new Date(1434360957*1000);
var formatted = dateTime.toGMTString();
https://jsfiddle.net/sp57Lnpf/
Use the date function. You need to specify the format as the first parameter:
date("d-m-Y", $timestamp_date)
http://php.net/manual/en/function.date.php
Laravel also comes with Carbon you could use that if you wanted to for further manipulation of the data if you so required it.
http://carbon.nesbot.com/docs/

Extract only date part in a datetimein LINQ

We have some data in a DataTable and we are using the query like this to get what we need.
IEnumerable<Results> subResult = from query in datatable.AsEnumerable()
select new Results
{
Name = query.Field<string>("Name"),
Date = query.Field<DateTime?>("Date")
}
This above query returns what i need but date in full format (Ex: m/dd/yyyy hh:min:sec am/pm) but we need only date part of it(only mm/dd/yyyy need to be pulled in). When looked in the properties of this, couldn't find an implicit way to get it, please help me in getting this result. Thanks.
The DateTime class has all the properties you just mentioned. You should try to fix your display with the ToString("anyformatyouwant") formats.
What about this?
Date = query.Field<DateTime?>("Date").Date
This will give you just the date part:
IEnumerable<Results> sub-result= from query in datatable.AsEnumerable()
where new Results
{
Name = query.Field<string>("Name"),
Date = query.Field<DateTime?>("Date").Date
}
However if you just want to display the date somewhere (meaning you're not using this to do date grouping, etc. then I would just specify the display format in your UI layer.
AFAIK there is no Date Class in .net, if you want pure date you should write a Class for that, otherwise you can pull out the part as a string
DateTime.Now.Date.ToString("yyyy/MM/dd")
gives you the current date in string type, but if you get the .Date part it gives you a DateTime object which still has a time part showing AM 12:00:00:0 blah blah

Resources