I need to convert my "80814.89999999851" timestamp to a YY/MM/DD string.
I started by doing this:
var prova = info.originalEvent.timeStamp; //this path lead to: 80814.89999999851
var prova2 = new Date(prova);
Now I have it as a datetime (if i'm not mistaken) but i don't know how to keep just the year/month/day and then convert it to a string.
Can someone help me?
This timestamp is not seconds since unix epoch, its seconds since timeOrigin (most likely time since page was loaded), to convert it to date you need to obtain timeOrigin first. Take a look at this question: Knowing the time origin of event's timestamp.
https://developer.mozilla.org/en-US/docs/Web/API/Event/timeStamp
Related
I tried looking for an answer but could not find a particular answer which does with Timezone offset. Hence, posting the same really sorry if there are any answer present already.
I have a requirement in the project where I need to convert the time to UTC or GMT based on the time specified by the user and timezone offset value provided by the user. Basically, the user provides his local time and timezone offset value according to his local time. I need to convert that into UTC/GMT time.
I am using the Node.js and following is the data which is available in the Node.js Backend:
Time: 2020-11-05T15:00:00.000Z
Timezone offset value: +02:00
As the timezone offset value is +02:00 I need to subtract it from the time to convert it into the UTC format. so I can get the time as: 2020-11-05T13:00:00.000Z. I using the moment-js as well. Can someone please help me how can achieve this using the Node.js or using the Moment.js?
If the offset takes daylight saving time into account you should be able to do:
const the_date = '2020-11-05T15:00:00.000';
const offset = 2;
const utc_time = moment.utc(the_date).subtract(offset, 'hours');
It's not clear which way you are trying to convert. If you are converting from UTC to a fixed offset, you can use the utcOffset function, like this:
const m = moment.utc('2020-11-05T15:00:00.000Z').utcOffset('+02:00');
m.format(); //=> "2020-11-05T17:00:00+02:00"
Or - if you were trying to convert from +02:00, then you would include that offset in the input instead of the Z (Z means UTC). You would then just call the utc function, like this:
const m = moment('2020-11-05T15:00:00.000+02:00').utc();
m.format(); //=> "2020-11-05T13:00:00Z"
However you should be aware that an offset is not the same as a time zone. A time zone can have more than one offset, one of which will apply at a given point in time. Those offsets can change due to daylight saving time and for changes in standard time. Thus, asking a user to pick "his timezone offset value according to his local time" is problematic - as you may be applying that offset to the wrong point in time. See "Time Zone != Offset" in the timezone tag wiki for further details.
You should also understand Moment's project status, and possibly choose a different library.
After some more research and trying, I was able to convert it. Posting the answer in addition to Christians response (https://stackoverflow.com/a/64701083/7584240) so if anyone is looking for the answer they will have another option:
var moment = require('moment');
var time = "2020-11-05T15:00:00.000Z";
var timeoffset = "+02:00";
time = moment.utc(time).local().format('YYYY-MM-DDTHH:mm:SS.000');
time = moment(time, 'YYYY-MM-DDTHH:mm:ss.000').subtract(timeoffset).format('YYYY-MM-DDTHH:mm:ss.000') + 'Z';
I've got a form where I input an event that starts at a certain time. Let's say 9am.
To assign a date/time object I'm using MomentJs. The issue comes when displaying it in different time-zones.
In London will show up 9am as intended - in Kiev will show 11am.
How can I make MomentJS and the browser ignore which timezone is relevant for the user, and just displaying the time I'm giving?
Here's my code:
<p>
Start time:
{moment(event.startDate).format("HH:mm")}
</p>
Assuming you have stored the date as utc (which in this case you probably should have), you could use the following:
moment.utc(event.startDate).format("HH:mm")
Let me provide an alternative answer in Vanilla JavaScript. If you want to make it timezone 'neutral', you can first convert it to UTC using toISOString().
const current = new Date();
const utcCurrent = current.toISOString();
console.log(utcCurrent);
If you want to convert it to a specific timezone, such as London, you can use toLocaleString(). Do take note of the browser support for the timezone though.
const londonTime = new Date().toLocaleString('en-US', { timeZone: 'Europe/London' })
console.log(londonTime);
What you want is a normalized Datetime. This can get a little confusing since the concept of timezones is a rather arbitrary construct.
I like to think of Datetime values as "absolute" and "relative". An "absolute" Datetime is one that is true regardless of which timezone you're in. The most common example of these are UTC(+000) and UNIX Time (also known as Unix epoch, POSIX Time or Unix Timestampe).
UTC is pretty obvious. Its the current time at +000 timezone. UNIX time is a bit more interesting. It represents the number of seconds that have elapsed since January 1, 1970.
You should always store data, in both client and backend, as an "absolute" time. My preference is UNIX time since its represented as a single integer (nice and clean).
moment.js does this for you. When you instantiate your moment object, you can use:
var date = moment.utc(utcString)
or for Unix Time
var date = moment.unix(unixInt)
You can then use this object to display the date in any form you wish:
console.log(date.tz.("America/Toronto"))
The only way I could solve this is by removing the timezone and milliseconds info from the string. I used date-fns lib but I imagine moment will work the same way.
import { format } from 'date-fns'
const myDateTimeString = '2022-02-22T19:55:00.000+01:00'
const dateTimeWithoutTimezone = myDateTimeString.slice(0, 16) // <- 2022-02-22T19:55
format(new Date(dateTimeWithoutTimezone), 'HH:mm')
I'm trying to convert foreign time to Local Time. I'm getting a date and time in Europe/London. Currently I'm using moment-timezone to get my code working, however its giving me a wrong output.
resultDate = new moment('2017-06-30T22:10:00').tz('Europe/London').format('YYYY-MM-DD HH:mm:ss');
I think the code thinks that the date input is already in local time where I need to convert it into Europe/London which would give a local result, where as what I want is to actually convert the foreign time to local time.
In short the date and time as my input (2017-06-30T22:00:00), I am expecting it to be 7 hours in advance (2017-07-1T05:00:00) since I currently live in Asia/Manila, 7 hours in advance to London. However I'm getting 2017/06/30 15:00:00 +0100 as my result.
Is there a way for me to do this by utilizing the information 'Europe/London' or 'Asia/Manila' as seen in my code?
You can use moment.tz to parse your input as Europe/London time and then use the tz function to convert it to Asia/Manila.
The first parses your input using the given timezone while the latter convert a moment objet to a given timezone.
Here a working sample:
// Parse input considering as London tz
var timeInLondon = moment.tz('2017-06-30T22:10:00', 'Europe/London');
// Converting input to Manila
var timeInManila = timeInLondon.tz('Asia/Manila');
// Show result
console.log(timeInManila.format('YYYY-MM-DD HH:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.11/moment-timezone-with-data-2010-2020.min.js"></script>
Hi im using moment js to convert this string 20:00 I tried:
var a = moment("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00+01:00
the problem when I store in mongodb it become
2016-09-10T19:00:00.000Z
I want to store 2016-09-10T20:00:00.000Z
anyway can explain why please ?
When you say that you want to store 2016-09-10T20:00:00.000Z what you are saying is that you want to assume that your date and time is UTC.
To assume that the date you are parsing is a UTC value, use moment.utc
var a = moment.utc("20:00", "HH:mm")
console.log(a.format()) // 2016-09-08T20:00:00Z
Note that when you parse a time without a date, moment assumes the current date. This may not be the behavior that you want.
I'm also not sure if you want a UTC date (which is what you are saying), or a local date without an offset indicator. If you want a local date without an offset indicator, simply use a format without an offset:
moment.utc("20:00", "HH:mm").format('YYYY-MM-DDTHH:mm:ss.SSS')
"2016-09-08T20:00:00.000"
If you are dealing with local dates that do not have a time zone association, I recommend using moment.utc to parse, as this will ensure that the time does not get shifted to account for DST in the current time zone.
For more information about how to parse dates into the time zone or offset that you would like in moment, see my blog post on the subject.
This it how it should look:
var a = moment("20:00", "HH:mm")
console.log(a.utcOffset('+0000').format())
<script src="http://momentjs.com/downloads/moment.min.js"></script>
Doe, the problem is that you are using timezones when you create the date.
MomentJS uses your current timezone automatically.
Mongo however saves the time as it would be in another timezone.
Therefore, if you want the two strings to format the same way, you need to set the timezone.
I got this problem when dealing with date time conversion. I have timestamp data from postgreSQL database with format like this one
"2011-04-04 19:27:39.92034"
In order to display it in highcharts, I have to convert it to date or time object. Without milliseconds, I easily convert it with Date.js
But milliseconds can't be handled with that library. I tried also with Date.parse but always got NaN.
Any solution for this problem? Thank you
JS built in Date class should be able to handle this, and getTime() can return milliseconds since start 1970 (UNIX time). Watch out for time zone issues though; the constructor may interpret the date/time as being local, but getTime()'s milliseconds since 1970 may be in UTC, baking in a conversion that is difficult to remove.
new Date("2011-04-04 19:27:39.92034").getTime()
1301941659920
Many ways to Rome. The given code will return '(datestr=) 2011-4-4 19:27:39.92'. Is that what you look for?
var darr = '2011-04-04 19:27:39.92034'.split('.')
, dat=new Date(darr[0])
, datestr = '';
dat.setMilliseconds(Math.round(darr[1]/1000));
datestr = [ [dat.getFullYear(),dat.getMonth()+1,dat.getDate()].join('-')
,' ',
[dat.getHours(),dat.getMinutes(),dat.getSeconds()].join(':')
,'.',
dat.getMilliseconds()
].join('');
Can't you just cut of the last 6 chars of that string? You might then round the miliseconds and eventually add a second to you time object.
This is simpler and in one line:
new Date('01/09/2015 06:16:14.123'.split(".")[0])