I am saving current date in mongodb with new Date(), But it is storing the date with the current time like below:
ISODate("2018-12-04T13:34:03.510+05:30")
I want to save only the date with above format, but without timezone for comparison purpose. Please tell me how can i do this?
You can use this package. This allows you to save dates in Mongo without having to worry about time zones shifting the date.
https://www.npmjs.com/package/mongoose-dateonly
You can save the date in the format of epoch time. And you can perform all kind of range queries on it.
var date = new Date('2018-12-04T13:34:03.510+05:30')
var userTimezoneOffset = date.getTimezoneOffset() * 60000;
new Date(date.getTime() - userTimezoneOffset);
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 have a date in YYYY-MM-DD format and I want to convert it into a timestamp like 1645985084088 using javascript. Can someone help me out?
To ensure consistency regardless of the timezone of the user:
let date = '2022-03-02';
new Date(date + 'T00:00Z').getTime();
This will give the UTC timestamp for midnight at the start of the given date.
Using .getTime() you can covert a Date object to a timestamp in JavaScript:
let date = new Date("02-03-2022");
console.log(date.getTime());
I have this application where I want to use you date, but the problem is that the date is not working as I expect.
I create a date object like this:
// Get today's date
today: function () {
// Create a new date
var date = new Date();
// Set to midnight
date.setHours(0, 0, 0, 0);
// Return our date
return date;
},
and If I output that date in my view I get yesterdays date at 23:00 hours....
Which looks like this:
2015-07-08T23:00:00.000Z
Does anyone know how I can get the date to be formatted properly?
Update
Just to elaborate a bit, I want to use the date to compare against records in the database. These records have the date applied to them, because the JavaScript is showing the local date time, it is not comparing correctly. Also there is a case where I am saving that date and I don't want it to save the local date.
based on your culture setting you can use the
date.toLocaleDateString()
this will give localized string format back
date.toUTCString();
date.toLocaleString();
date.toLocaleDateString();
date.toDateString();
date.toISOString();
Find your answer here :) And the best option is to use momentjs http://momentjs.com/
So, I ended up creating this function:
// Converts a date to a timeStamp
this.convertToTimeStamp = function (dateTime) {
// Get just the date
var date = dateTime.toDateString();
// Get the timestamp
var timeStamp = Date.parse(date);
// Return our timeStamp
return timeStamp;
};
If my understanding is correct, that should create the same date no matter what timezone / locale you are in.
I am working with momentjs and converting dates to different time zones using convertedDate = moment().utcOffset(timezone).format(). This works well but it is a string and I need to transform it to date object.
I've tried new Date(convertedDate) and moment().utcOffset(timezone).toDate() but that returns my current timezone as a date object. How can I keep the converted timezone?
So I wasn't very far off. The format needs to exclude timezone for it to work. This code finally worked how I needed it to.
convertedDate = new Date(moment().utcOffset('-4').format('YYYY-MM-DD HH:mm'));
A cleaner approach to get a native Date object with time according to the timezone, using moment would be following:
convertedDate = moment.utc(moment.tz(timezone).format('YYYY-MM-DDTHH:mm:ss')).toDate()
PS: assuming two things
you have imported both 'moment' and 'moment-timezone'.
value of timezone is given like 'Asia/Kolkata' instead of an offset value
This should work:
I have the same issue. Just get the Date as a string using the same approach that you are using. Let's say your date is, for example: '2018-08-05T10:00:00'.
Now you need the Date object with correct time. To convert String into object without messing around with timezones, Use getTimezoneOffset:
var date = new Date('2016-08-25T00:00:00')
var userTimezoneOffset = date.getTimezoneOffset() * 60000;
new Date(date.getTime() - userTimezoneOffset);
getTimezoneOffset() will return either negative or positive value. This must be subtracted to work in every location in the world.
I need to get the value of 00:00:00 AM GMT(12am) for the current day and then convert it to unix time. How would/should I go about doing that in javascript? Is there an outside data source that is more reliable then server time? I will be doing this in node on the server.
Thanks!
EDIT: This is what I did. Do you see any problems with this? Thanks again!
date = new Date()
start_date = Date.UTC(date.getFullYear(),date.getUTCMonth(),date.getUTCDate()) / 1000
Your method is right, but you have got a nasty bug in there, you are mixing local year with UTC date and month, for a few hours around new year, depending on time zone, the local and the UTC year is different, so if you use the wrong year your result will be a whole year off.
There are two interpretations of your question. Either you want a result based on the local time, so the result at any given time will depend on the time zone. Or you want a result based on UTC time that is the same no matter time zone, but sometimes for some users the result will not be the local date.
Local time:
date = new Date()
start_date = Date.UTC(date.getFullYear(),date.getMonth(),date.getDate()) / 1000
UTC:
date = new Date()
start_date = Date.UTC(date.getUTCFullYear(),date.getUTCMonth(),date.getUTCDate()) / 1000
This is a good place to start:
var now = new Date();
var then = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var epoch = then.getTime();
Not sure what you want to do about DST, so you'll need to look at:
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset
Edit: To allow for different timezones:
var off = now.getTimezoneOffset() * 60000; /* tz is in mins so multiply to ms */
var midnight = new Date(then.getTime() - off);
var epoch = midnight.getTime();
These links have some good answers. I love epochconverter.com it's saved me many hours of frustration. The essence of the answer is to use the Javascript Date object to handle all the nastiness of converting dates around. This is generally what you should do in any languages. If you are doing date manipulation by hand you will get it wrong.
http://www.epochconverter.com/programming/#javascript
https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Date
This is what I ended up doing for anyone else looking at this question.
date = new Date()
start_date = Date.UTC(date.getFullYear(),date.getUTCMonth(),date.getUTCDate()) / 1000
Please let me know if you see any reason this wouldn't work.