Home > Back-end >  Trying pass datetime to the api
Trying pass datetime to the api

Time:07-26

I am trying to pass the current client local date time to API by using the GET method, getting the error,

UI:

  const localDateTime = new Date().toLocaleString();

  await fetchDocument(`${process.env.REACT_APP_API_URL}/api/projects/${projectNumber}/${localDateTime}/${currentProjectPhase}`,
  setIsBodDownloading,
  instance,
  history
  );

API:

    [HttpGet("{projectNumber}/{localDateTime}/{projectPhase?}")]
    public async Task<ActionResult> BasisOfDesign(
        [FromServices] IDbContextFactory<APIDbContext> serviceScopeFactory,
        [FromServices] ICrmProjects crmProjects,
        [FromServices] IConfiguration configuration,
        [FromServices] IEmployeeContext employeeContext,
        string projectNumber,
        string localDateTime,
        string projectPhase = null,    
        CancellationToken cancellationToken = default)
    {  

        .....
        ....
     }

I am getting the below error.

{"errors":[{"message":"Either the parameter query or the parameter id has to be set.","extensions":{"code":"HC0013"}}]}

I cannot use the server time, and I need to use only the client date and time for processing the results. I am looking into this type of string "12-19-2012, 7:00:00 PM"

Could anyone please let me know, Where I am doing wrong with the above code?

CodePudding user response:

this line of code const localDateTime = new Date().toLocaleString(); is producing

7/25/2022, 9:59:46 PM 

wich is not valid in URL.

try changing it to a valid form maybe like this :

var dateObj = new Date();
var month = dateObj.getUTCMonth()   1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
newdate = year   "/"   month   "/"   day;
console.log(newdate) // producing valid form to be used in URL like this 2022/7/25

CodePudding user response:

new Date().toISOString() will produce ISO format which the API's generally accept( ISO-8601).

Console.log(new Date().toISOString()) 

Result:'2022-07-25T22:20:38.413Z'
  • Related