Using Date Getting Set 1970

In one of my workers, I am trying to set a field to the current date and every time I do, this is what it is getting set to: 1/1/1970.

Here is what I am using: let currentDate = new Date().toLocaleDateString();

What am I am missing?

Can you share your worker code? As long as you create the date in your request scope, you’ll be fine. If you create it in the global scope, it will be 0.

Here’s an example:

https://cloudflareworkers.com/#47b4f55cd7752f013f08b334cd26421c:about:blank

1 Like

The example was helpful, and it looks like it was in the wrong scope. When I move it to the right scope, there is an issue with the syntax as I am trying to build an a json aray to post back.

async function setSignature(user_id){
const currentDate2 = new Date();
var user_data = {
“user”: {
//“alias”: “Customer Care Agent”,
//“signature”: “{{agent.name}}\n{{agent.organization}}\n+1 (123) 456-7890”,
//“details”: “”,
“notes”:“Test”,
“user_fields”: {
“hire_date”: ${currentDate2.toLocaleDateString()}
},*
“verified”: true,
}
}

I’m not sure exactly what you’re trying to do, so have made some assumptions, but here’s an example with your code:

https://cloudflareworkers.com/#89b7ddc2fef86f4bcd7613e879536828:about:blank

export default {
  fetch: handleRequest
}

const badDate = new Date();
async function setSignature(user_id){
  const currentDate2 = new Date();
  var user_data = {
    "user": {
      //"alias": "Customer Care Agent",
      //"signature": “{{agent.name}}\n{{agent.organization}}\n+1 (123) 456-7890",
      //"details": "",
      "notes":"Test",
      "user_fields": {
          "hire_date": currentDate2.toLocaleDateString()
      },
      "verified": true,
    }
  }
  return user_data;
}

async function handleRequest(request) {
  const user = await setSignature('some_id');
  return Response.json(user)
}

There were quite a few issues with your JavaScript syntax.

Wow! That worked perfectly, thanks for your help!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.