I get a date with string type from API and then I parse it to a date type so that I can use it for a count down.
I want to add 30 days to the date that I've got from API.
Here is the code that I parsed
const time = Date.parse("2020-12-30T18:35:43");
I've already read this question and I tried to implement it
Add 10 seconds to a Date
but react does not recognize the getDate
if you need more information, please let me know
You need to wrap your parsed date with a new Date()
const time = new Date(Date.parse("2020-12-30T18:35:43"));
// As mention by other comments, this is enough
// const time = new Date("2020-12-30T18:35:43");
time.setSeconds(time.getSeconds() + 10) // 1609349753000
setSeconds and getSeconds are method of the Date, you was trying to execute them on a number.
EDIT :
Answer can be found here
In a general way you should use date-fns for date manipulations ;)
you can also setDate to your existing date. Check following code.
const date = new Date("2020-12-30T18:35:43");
date.setDate(date.getDate() + 30);
Related
I'm trying to add the current time to an existing date but I'm not sure how to do it.
I'm importing stuff into a Postgres database and need a ISO string to update the "updatedAt" column, the imported stuff only has a date like this tho: "2022-03-15", no time.
How would I add the time to this and turn it into a proper ISO string for my database?
const date = new Date('2022-03-15')
const iso = date.toISOSTring() // how to add the current time?
-
Should look like this: "2022-03-15 09:36:54.292613"
Thank you! :)
Try to use dayJs and add the time that you need, https://day.js.org/docs/en/parse/string
dayjs('2018-04-04T16:00:00.000Z')
dayjs('2018-04-13 19:18:17.040+02:00')
dayjs('2018-04-13 19:18')
You can set the time units into date from the current date-time i.e. new Date().
const date = new Date("2022-03-15");
const now = new Date();
date.setHours(now.getHours());
date.setMinutes(now.getMinutes());
date.setSeconds(now.getSeconds());
date.setMilliseconds(now.getMilliseconds());
console.log(date.toISOString());
console.log(date.toISOString().replace("T", " ").replace("Z", " "));
I am able to get the timestamp using this line in my index.js.
var now = admin.firestore.Timestamp.now();
I just want the timestamp that's 1 hour before now.
I've tried this and hasn't worked.
var old = now(now.seconds-1*60*60,milliseconds);
Also tried but it returns just the seconds instead of a timestamp
var old = admin.firestore.Timestamp.now().seconds - 1*60*60;
Any ideas on how to get the timestamp of an hour before?
Firestore's Timestamp class doesn't offer the ability to do "date math". You should do any date math using a native date type for your language, then convert that to a Timestamp when you're done.
Since you tagged this JavaScript, you could use a normal Date object to do the math:
const date = new Date(Date.now() - 60*60*1000)
The convert that to a Timestamp with Timestamp.fromDate():
const timestamp = admin.firestore.Timestamp.fromDate(date)
Seems like a simple question, but all the timezone ins and outs in JS are causing me a bunch of headaches.
Basically, if I have a date like the following:
2018-04-06
I want to be able to get the next day's date as such:
2018-04-07
I found the following snippet on SO for doing this (kind of):
var date = new Date('2018-04-06');
date.setDate(date + 1);
The problem is that I'm getting the date back with the adjusted timezone, and because I'm in the US ET timezone, it's giving me that date minus five hours, which is actually the same day as where I started.
I've been through countless SO posts trying to find an answer to this seemingly simple question, but for any given date, regardless of the timezone the user is in, how do I get the next day's date in YYYY-MM-DD format? Thank you.
Strings in the format YYYY-MM-DD are parsed as UTC so in this case, do everything in UTC (see Why does Date.parse give incorrect results? and How can I add 1 day to current date?).
The toISOString method will return the string in the required format, just trim the redundant time part, e.g.
let s = '2018-04-06';
let d = new Date(s);
d.setUTCDate(d.getUTCDate() + 1);
console.log(d.toISOString().substr(0,10));
Did you try with the UTC date?
var date = new Date('2018-04-06');
console.log(date.toUTCString());
date.setDate(date.getDate() + 1);
console.log(date.toUTCString());
As it was suggested by #chrisbyte, have your tried to use toUTCString method instead of toString() method ?
As a reminder , toString is the default used when you display the date object withim the console for example
I think the "problem" you're assuming is just an incomplete understanding how Date.toString() method behaves: this method seems to to return string representing a Date object but seems to use timezone as mentionned here (on the comment in 1st example)
Here my snippet to understand more:
const originalDate = new Date('2018-04-06');
// retrieving the original timestamp
const originalTimestamp = originalDate.valueOf()
// displaying the original date (non UTC / UTC)
console.log(`original date (timezone dependent): ${originalDate.toString()}`)
console.log(`original date (timezone independent): ${originalDate.toUTCString()}`)
// we add one more day
originalDate.setDate(originalDate.getDate() +1)
const dayAfterTimestamp = originalDate.valueOf()
// displaying the original date (non UTC / UTC)
console.log(`updated date (timezone dependent): ${originalDate.toString()}`)
console.log(`updated date (timezone independent): ${originalDate.toUTCString()}`)
// check the differences (in milliseconds)
console.log(`difference: ${(dayAfterTimestamp-originalTimestamp)}`)
// displaying the original format (timezone independent)
At last if you want to return the date string as a YYYY-MM-DD format you may have to implement it yourself :-/ , or use toLocaleFormat method but it isn't standardized.
The logic would be to add 24 hours in milliseconds to the current time. As an example:
var myDate = new Date();
var oneMoreDay = new Date();
oneMoreDay.setTime(myDate.getTime() + 86400000);
console.log(myDate.getDate());
console.log(oneMoreDay.getDate());
An additional day has been added to the oneMoreDay variable. In your specific example you just wanted to add one more day to the ORIGINAL variable, so i'd do something such as:
date.setTime(date.getTime() + 86400000);
I have this string date and I want to use it in a date type for querying in sequelize.
let date = "2019-08-08T12:53:56.811Z"
let startDate = new Date(date)
console.log(startDate)
->> 2019-08-07T12:53:56.811Z
when I am trying to insert to db.it changes with another hour.
let newTask = Task.create({ time_to_deliver: startDate})
console.log(newTask.time_to_deliver)
->> 2019-08-08 17:23:56
what is this? is it something about timezone and UTC time stuff?
I think if you will make the "startDate" key in your DB of 'Date' type instead of 'String' type it will work. I have checked this with MongoDB and it worked for me.
"startDate: {type: Date}"
according to #barbsan answer . sequelize changes the date to toLocalString() format. in querying return data it turns to the actual date.
you can simply use this javascript functions for better enhancement
var d = new Date("July 21, 1983 01:15:00");
var n = d.getDate(); //this will give you 21
there is also a similar function available - getMonth(),
First, get this date day and month and time [for the time you can use these functions => 1. getTime() and 2. new Date().toISOString()]
and then send these things separately and combine them whenever you want retrieve.
There is the also second approach;
First, convert your date and time into a timestamp and enter that timestamp into the database.
var myDate="26-02-2012";
myDate=myDate.split("-");
var newDate=myDate[1]+"/"+myDate[0]+"/"+myDate[2];
alert(new Date(newDate).getTime());
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 complex 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.com/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 come out as 1970 here?! https://currentmillis.com/
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() becomes .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.
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.com/tstreamDOTh/b8b741853cc549f83e72572886f84479
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.