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());
Related
I have a date selector and a time selector, and I'm trying to figure out how I can combine their outputs to make a single ISOString so that I can use with the google calendar API.
Here's what I've tried:
//date = 2022-05-18
//time = 14:22
const apptdate = new Date(date)
const timeSplit = time.split(':')
apptDate.setHours(timeSplit[0])
apptDate.setMinutes(timeSplit[1])
What I notice is when I console.log(apptdate) this is the output I get: 2022-05-17T18:22:00.000Z
I'm not sure why it changes the day from May 18 to May 17, and the time from 14:22 to 18:22.
Does anyone have a solution for this? Or even a completely different way of combining date and time to one string (other than using a datetime-local input format, I want to keep the date and time separate in my database).
"2022-05-18" is parsed as UTC but apptDate.setHours(timeSplit[0]) sets the local hour. So if the host has a negative offset, the local date is 17 May and the time is set to the local hour on 17 May, not UTC hour on 18 May.
Instead use setUTCHours and setUTCMinutes.
let date = '2022-05-18';
let time = '14:22';
let apptDate = new Date(date);
let timeSplit = time.split(':');
apptDate.setUTCHours(timeSplit[0]);
apptDate.setUTCMinutes(timeSplit[1]);
// 2022-05-18T14:22:00.000Z
console.log(apptDate.toISOString());
PS. There was also a typo: let apptdate then later apptDate.
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);
When i use the new Date() instance i get something like
2020-12-10T12:30:18.108Z
the problem is that by me the hour on my local machine is 13:30 - but it gives me one hour earlier so 12:30 in the current time. How can i customize to give me the right time of my local machine ?
Also i don't know the meaning of the last four character after the . sign
108Z
all i need is to insert in my db the hour and the date without this signs
2020-12-10T12:30:18
is there any other solution except manupulating the string directly ?
You're using UTC Date I guess. You can try to use .toLocaleString() on your date time object.
Maybe use like that
var datetime = new Date();
var now = datetime.toLocaleString();
This should get you something like this: 10/12/2020, 8:47:15 AM
UPDATED
Another option if you want to maintain format and just remove T and Z characters is to replace string
You can try this:
var datetime = new Date();
var now = datetime.toISOString().replace('Z', '').replace('T', '');
Ok, so what I want to do is to retrieve some info from an input element, which is of type date. I then store this in a .json file, and I parse it later on to retrieve the data upon program start. After that, I want to use the date.getDay() function to figure out what day that date falls on.
I have only seen examples using var d = new Date(), and something tells me that the 'format' is different when using new Datethan using document.getElementById("dateInput").value;
Anyone catch my drift?
To sum up, I want to be able to find the day from the values outputted by an input type = "date" element.
Date.getDay() does not accept the default type (String) that gets returned by the elem.value()-call.
You have to pass a Date.
So you can convert any legit string to a date using the "new Date()" constructor.
let dateString = "2018-03-08"
let dateFromString = new Date(dateString) // working
Please have a look at this example code:
let date = new Date(mydate.value)
let day = date.getDay()
console.log(day)
<input id="mydate" type="date" value="2018-03-08">
You could also find some helps in the docs:
Date JavaScript
JavaScript Date Reference
Well, Date.getDay() does not accept any input, it is an "instance method", if you will, of the Date class. This means a Date object must be instantiated before you can call getDay() on it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDay
I think what you're trying to do is something along these lines:
<input id="js-date" type="date" value="2018-03-08">
let el = document.getElementById('js-date')
let date = new Date(el.value)
alert(date.getDay())
I want to find all the users created up until yesterday. This is my code to make the query string:
var today = new Date();
var a = today.getDate();
a--;
today.setDate(a);
var yesterday = today.toDateString();
and it returns something like: Sun Jan 17 2016... which IS yesterday's date, but the db stores the date in iso format like: "date" : ISODate("2016-01-13T13:23:08.419Z") so what I get is 2016-01-13T13:23:08.419Z. My problem now is that I can't use the yesterday variable to query the db for the users I need AND even if I could, I don't know how to find every registration not including the ones that took place today.
Any help? Thank you very much!
You are generating a date on the front end, and then pushing it back a day, which is totally fine for a lot of circumstances -
For this, since you are trying to find DB entries that occured, perhaps try querying the database with a timestamp ranged pulled from the ID's of each document in your database.
Here is some documentation on how to do that from mongoDB. https://docs.mongodb.org/v3.0/reference/method/ObjectId.getTimestamp/
I've also provided some additional resources that may help you figure out exactly what to query in regard to this method:
https://steveridout.github.io/mongo-object-time/
https://gist.github.com/tebemis/0e55aa0089e928f362d9
Some psuedo code:
1. Query documents in the database
2. Get a timestamp from the ID's of the documents in the database
3. Set a range of the timestamps
4. Compare returned timestamps vs a timestamp range variable (yesterdays date in this case)
5. Have the DB return only documents that are within the range
I hope this helps!
Try this, using Moment.js:
yesterday = moment().add(-1, 'days');
db.users.find({ "date": { "$lt": yesterday }});
Create a date object that represents the start of day today, use it to query your collection for documents where the date field is less than that variable, as in the following example
var start = new Date();
start.setHours(0,0,0,0);
db.users.find({ "date": { "$lt": start }});
This will look for users created up until end of day yesterday.
momentjs is a super handy utility for doing manipulations like this. Using the library, this can be achieved with the startOf() method on the moment's current date object, passing the string 'day' as arguments:
Local GMT:
var start = moment().startOf('day'); // set to 12:00 am today
db.users.find({ "date": { "$lt": start }});
For UTC:
var start = moment.utc().startOf('day');