最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get current timestamp in NodeJS in DD-MM-YY-23:59:42 format - Stack Overflow

programmeradmin0浏览0评论

I am running NodeJS 8 in AWS Lambda and want to timestamp and attach to an S3 the current day, month, year and current time when the function runs.

So if my function was running now, it would output 220619-183923 (Todays date and 6.39pm and 23 seconds in the evening.)

For something a little plex like this do I need something like MomentJS or can this be done in pure Javascript?

Eventually this will make up a S3 URL such as .co.uk/BN-220619-183923.pdf

UPDATE The webhook appears to have some date/time data albeit in slightly different formats that weren't outputted in the Lambda function, so these could prove useful here. Can ':' be used in a URL and could the UTC which I assume is in milliseconds be converted into my desired format?

createdDatetime=2019-06-22T18%3A20%3A42%2B00%3A00&
date=1561231242&
date_utc=1561227642&

Strangely, the date_utc value which is actually live real data. Seems to e out as 1970 here?! /

I am running NodeJS 8 in AWS Lambda and want to timestamp and attach to an S3 the current day, month, year and current time when the function runs.

So if my function was running now, it would output 220619-183923 (Todays date and 6.39pm and 23 seconds in the evening.)

For something a little plex like this do I need something like MomentJS or can this be done in pure Javascript?

Eventually this will make up a S3 URL such as https://s3.eu-west-2.amazonaws./mybucket.co.uk/BN-220619-183923.pdf

UPDATE The webhook appears to have some date/time data albeit in slightly different formats that weren't outputted in the Lambda function, so these could prove useful here. Can ':' be used in a URL and could the UTC which I assume is in milliseconds be converted into my desired format?

createdDatetime=2019-06-22T18%3A20%3A42%2B00%3A00&
date=1561231242&
date_utc=1561227642&

Strangely, the date_utc value which is actually live real data. Seems to e out as 1970 here?! https://currentmillis./

Share Improve this question edited Jun 22, 2019 at 18:31 sigur7 asked Jun 22, 2019 at 17:52 sigur7sigur7 9362 gold badges14 silver badges40 bronze badges 1
  • Please see my answer. I've included query params. The 1970 thing is covered in my solution. It is because AWS shortens the epoch. Add three zeros to the end of the timestamp and it will work. – Randy Casburn Commented Jun 22, 2019 at 18:39
Add a ment  | 

3 Answers 3

Reset to default 1

You don't need moment. I have included a solution that is quite verbose, but is understandable. This could be shorted if needed.

Since you are using S3, you might also consider using the UTC versions of each date function (ie. .getMonth() bees .getUTCMonth())

Adjust as needed:

createdDatetime= new Date(decodeURIComponent('2019-06-22T18%3A20%3A42%2B00%3A00'))
date=new Date(1561231242 * 1000);
date_utc=new Date(1561227642 * 1000);

console.log(createdDatetime, date, date_utc)

const theDate = createdDatetime;
const day = theDate.getUTCDate();
const month = theDate.getUTCMonth()+1;
const twoDigitMonth = month<10? "0" + month: month;
const twoDigitYear = theDate.getUTCFullYear().toString().substr(2)
const hours = theDate.getUTCHours();
const mins = theDate.getUTCMinutes();
const seconds = theDate.getUTCSeconds();
const formattedDate = `${day}${twoDigitMonth}${twoDigitYear}-${hours}${mins}${seconds}`;

console.log(formattedDate);

UPDATE based upon your update: The code here works as long as the input is a JavaScript Date object. The query parameters you provided can all be used to create the Date object.

What is the goal of creating this date string? If you just need it as a human-readable timestamp, running this would be enough:

new Date().toISOString()

That gives you the UTC time on the server. If you need the time to always be in a particular time zone, you can use moment.

You can definitely use MomentJS to achieve this. If you want to avoid using a large package, I use this utility function to get a readable format, see if it helps

https://gist.github./tstreamDOTh/b8b741853cc549f83e72572886f84479

发布评论

评论列表(0)

  1. 暂无评论