I have encountered interesting problem:
I have an timestamp with value 1500400800
I want to get staart of day and end of day of this timestamp. By using moment.js I have written this code:
let timestamp = 1500400800;
let momentDate = moment.unix(timestamp);
let start = momentDate.startOf('day').unix();
let end = momentDate.endOf('day').unix();
console.log(timestamp + " " + start + " " + end);
My own PC returns for 1500400800 these values:
1500400800 1500400800 1500487199
But virutal machine in my hosting returns this:
1500400800 1500336000 1500422399
They are different. There are six hours difference between two platforms.
How to make virtual hosting machine get timestamps just like my local PC gets?
Moment assumes time relative to the time of the local system. Your virtual server either has an incorrect clock or is located in another timezone. You can either work exclusively in UTC by telling moment to convert to UTC, or you might look into moment-timezone which is an extension of moment built specifically for working in multiple timezones.
Related
I am migrating event data from an old SQL database to a new Mongo database, using NodeJS. However, whoever set up the SQL database created all of the dates for the events, made the times in PST/PDT, but the database believes they are in UTC time.
For Example:
A date from the SQL database may be: 23-APR-10 which MomentJS shows as: 2010-04-23T21:00:00Z when 21:00:00 is the PST time.
Is it possible to use pure JavaScript/MomentJS/NodeJS or a different npm module to change the timezone on the DateTime string without modifying the time (i.e. 2010-04-23T21:00:00Z would become 2010-04-23T21:00:00-8:00)?
PS. Even though the SQL database only shows DD-MMM-YY but returns a DateTime string when I query it.
Following the line of inquiry in the question comments, it seems your problem is that due to the timezone mishap, the timestamps stored in the db are stored without the timezone offset, and since your desired timezone is PST (UTC-8hours), the timestamps are 8 hours ahead, for instance, what should have been 2010-04-23T13:00:00Z has become 2010-04-23T21:00:00Z.
So what needs to be done here is that the utc offset for your desired timezone needs to be obtained and added to the date.
The offset in your case is known (-8 hours). However, we can fetch the correct offset of any desired timezone from the moment-timezone library.
const moment_timezone = require('moment-timezone');
//a sample timestamp you're getting from your db
const myDateObj = new Date("2010-04-23T21:00:00Z");
//timezone for PST as understood by moment-timezone
const myMomentTimezone = "America/Los_Angeles";
//offset for your timezone in milliseconds
const myTimezoneOffset = moment_timezone.tz(myMomentTimezone).utcOffset()*60000;
//perfom the correction
const getCorrectedDateObj = (givenDateObj) => new Date(givenDateObj.valueOf() + myTimezoneOffset);
console.log(getCorrectedDateObj(myDateObj));
You may notice that we are actually changing the timestamp, because the given timestamp and the requried timestamp are, due to the nature of the error, essentially different timestamps. Moment-timezone is only being used here to fetch the offset, it's not used to "convert" anything.
Anuj Pancholi's answer is correct, however; the old SQL database I'm using seems to have a lot of quirks, so I had to modify his answer to get my code working.
My Solution:
function getCorrectedDateObj(myDateObj){
const timezoneOffset = momentTimeZone.tz(timezone).utcOffset() * 60000;
const dt = new Date(myDateObj.valueOf() + timezoneOffset / 2);
dt.setHours(dt.getHours() + 12);
}
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.
var d = new Date();
"**Zone** - `" +
d.toTimeString().replace(/\d+:\d+:\d+\s+/, '').replace(/[G][M][T].\d+/, '') +
"` **Time** - " +
"`" + d.toLocaleTimeString() +
"` **Date** - " + "`" +
d.toLocaleDateString() + "`"
This is a string of mine which tells me the region, time and date. I am using it on a discord bot and am attempting to make them be different depending on the user's region. I'm not sure how to accomplish this, but for visual example...
The code stated above returns me -
Zone - (AUS Eastern Standard Time) Time - 16:49:28 Date - 2018-5-18
which I am satisfied with, however, for a test I got a foreign friend to hop on and it sent the same thing when what I am trying to make the result be is their region so...
Zone - (AMERICA Western Standard Time) Time - 02:29:09 Date - 2018-5-18
Anybody have any ideas?
EDIT: Within a new project of mine, I discovered the user of Intl.DateTimeFormat() which essentially returns an Array. There is an instance method for this function known as .resolvedOptions(), returning an object that has many values within. I simply went ahead and added .timezone which returned something similar to "Australia/Queensland".
var region = Intl.DateTimeFormat().resolvedOptions().timeZone;
I am assuming this code is run in a browser, and not in a server.
You cannot depend on d.toTimeString() and cutting out the timezone part to find a timezone, toTimeString can return different formats on different browsers. The specs has left the formatting to browser vendors.
You can get the timezoneOffset and map it to the timezone name, but wait, there is an issue. This may conflict with Day Light Savings time setting for regions. So you will need a historical set of data for Daylight Savings time all over world.
If you really want to do this, use moment with timezones, which can do the lookup for you and they have already created all mappings including DST for you.
I'm currently building a fantasy sports website where i need to save future events in my firebase database where i will have to account for that locations timezone.
My thinking was along the lines of calculating a unix timestamp using the date and time of the event & timezone then saving that into the database for future reference to count down to it etc.
I can't quite figure out how to make this work, i believe moment.js is the right move here but it has me confused if this is maybe the best option or not.
Any advice is greatly appreciated.
Ryann,
This is how i solved it using Moment Timezone
let timezone:string = this.round.timezone; // America/Los_Angeles
let date:string = this.round.eventDate // 2017-01-07
let gateOpenTime = this.round.gateOpenTime; // 12:00
let momentStringGate:string = date + ' ' + gateOpenTime; // 2017-01-07 12:00
this.round.gateOpenUnix = moment.tz(momentStringGate, timezone).unix(); //1483819200
Sorry if the title is a little convoluted. I'm bashing my head against the floor with times in NodeJS / Javascript. I can get the current UTC time like this:
var currentTime = Date.now();
I can get the current time for a user who is, for example, in the -3 timezone like this:
var offsetTime = Date.now() + (numTimeZone * 3600000);
But how do I get the local user time at, say, 6am, converted to UTC?
Practical application:
What I'm trying to do is create an auto-emailer which sends an email to a user at 6am in their local time. My server is in one timezone and they will be in another, so I'm trying to standardise it against UTC so every minute I can set my server to check the currentUTC time, then check what the user's 6am time is converted to UTC (local6am), and if the currentUTC > local6am then an email should be sent.
What's the best way to achieve this? Preferably without using a library if possible.
Utc to Local
moment.utc('2014-02-19 05:24:32 AM').toDate();
Local to utc
Read this documentation.
MomentJS is parsing the date as a locale date-time. If no hour is given, it is assuming midnight.
Then, you convert it to UTC, so it is shifted, according to your local time, forward or backwards. If your are in UTC+N, then you will get the previous date.
moment(new Date('02-19-2014')).utc().format("YYYY-MM-DD HH:mm").toString()
moment(new Date('02-19-2014 12:00')).utc().format("YYYY-MM-DD HH:mm").toString()
(or)
You can try this:
moment.utc('07-18-2013', 'MM-DD-YYYY')
moment.utc('07-18-2013', 'MM-DD-YYYY').format('YYYY-MM-DD')
You do not need to call toString explicitly.