I have an api return an expire time, which looks like a countdown. Im not familiar with milliseconds or whatever this is. The countdown said this number was around a few minutes. I thought it was a unix time, where you could subtract current time from 1970, then convert to minutes but that is giving me a crazy number..
What format is this?
1470041268756
There's nothing crazy about it. Well, nothing beyond normal UNIX craziness :-)
If you head on over to the Unix epoch converter and plug that value in, you'll see:
Assuming that this timestamp is in milliseconds:
GMT: Mon, 01 Aug 2016 08:47:48.756 GMT
which is certainly viable in terms of being a few minutes after you formed the question.
As you haven't given much information, I would have to guess that it is unix timestamp but in milliseconds. So if you divide by 1000 first (essentially remove the last 3 digits) and then convert you would get the following:
Mon, 01 Aug 2016 08:47:48 GMT
I would expect you can find a conversion function that works with milliseconds too though. In fact, with javascript the Date object will take this format (in milliseconds):
var date = new Date(1470041268756);
console.log(date); // Mon Aug 01 2016 09:47:48 GMT+0100 (GMT Summer Time)
They look like milliseconds since 01/01/1970 00:00
see here
you may use moment.js
alert(moment.unix(1470041268756/1000).format('MMMM Do YYYY, h:mm:ss a'));
<script src="http://momentjs.com/downloads/moment.js"></script>
Related
I'm using coingecko api and they state that -
Timestamps returned by this API are in UTC Timezone.
and the call I'm making:
'https://api.coingecko.com/api/v3/coins/'+'dash'+'/ohlc?vs_currency='+'eur'+'&days=365'
for example the timestamp (I think) that should be for today is 1579046400000:
new Date(1579046400000)
Wed Jan 15 2020 00:00:00 GMT+0000 (Western European Standard Time)
but today is:
+new Date()
1610300187790
Sun Jan 10 2021 17:36:27 GMT+0000 (Western European Standard Time)
I'm looking at the Date object in javascript https://www.quackit.com/javascript/javascript_date_and_time_functions.cfm
and I'm very confused. How do I convert this UTC timestamp to a normal unix timestamp?
I don't even understand what format the UTC one is, is it milliseconds? (to me it looks like seconds)
all UTC questions I can find the date does not look like this! (They look like strings)
I can't even figure out how to load this number into the date object:
new Date().UTC(579046400000)
VM392:1 Uncaught TypeError: (intermediate value).UTC is not a function
at <anonymous>:1:12
(anonymous) # VM392:1
new Date().toUTCString(579046400000)
"Sun, 10 Jan 2021 18:01:25 GMT"
new Date().
setUTCDate(579046400000)
NaN
Date.UTC(579046400000)
NaN
How do I load it into the date object?
The data is sorted from oldest to youngest. The first entry - which you assumed to be today - is actually the oldest one, roughly 365 days old. The last entry in the list is from today. So new Date(timestamps) is the correct way to handle these dates. (UTC and Unix timestamps are identical for everything after 1972 - I believe) – cuzi
I should not be dyslexic
#cuzi is 100% correct!
I have files with epoch time stamps such as 1564002293050. Using https://www.epochconverter.com/ this shows Wednesday, July 24, 2019 9:04:53.050 PM however my code shows Mon Apr 06 51531 02:24:10 GMT-0700 (Pacific Daylight Time). Why is this?
Because the times were generated in Unix, I multiplied by 1000 for ms. This value is then displayed.
time = 1564002293050;
var dateStamp = new Date(time* 1000);
Edit:
Ive referenced this post and several similar others. It is good to note that not multiplying it by 1000 will result with "Invalid Date".
Edit 2:
Figure it out. I was parsing the data but looks like I had to convert it to an integer parseInt(time) ended up fixing the issue. Sorry for the unrelated solution..
document.write(new Date(1564002293050));
Prints Wed Jul 24 2019 15:04:53 GMT-0600 (Mountain Daylight Time) (in my TZ).
Even this outputs the same in HTML, despite not syntactically declaring said variable.
time = 1564002293050;
var dateStamp = new Date(time);
document.write(dateStamp);
Are you doing this on the browser? Another Javascript engine?
The Date constructor takes the epoch time in milliseconds (https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Date). Your time is in milliseconds. No need to multiply it by 1000.
I've got a UTC date through an Ajax call, e.g. "/Date(1517216466000+0100)/",
which is when printing to the console: Mon Jan 29 2018 10:01:06 GMT+0100 (W. Europe Standard Time).
What I need to do, is to let the user change the timezones: I can easily do it with e.g. moment(myDate).tz("Japan").
Then I need to save the date in it's UTC format, which I am not able to do.
I've been experimenting with moment.utc(), but for the input above, it returns 1 hour less.
How to deal with this situation? In summary:
1. Get a UTC time from a webservice
2. Let the user change the timezones
3. Save the modified date in UTC (without the timezone)
Working demo: https://stackblitz.com/edit/angular-kqrct7?file=app%2Fapp.component.html
EDIT for clarification:
Let's just have a look at the hours. What I get the date from the WCF, is 10 o'clock. The browser interprets it as 10 o'clock BUT in GMT+1 so when I convert it to UTC, it becomes 9 o'clock.
I want it to be 10 o'clock as UTC. Then, if I modify the timezone and e.g. the minutes of this date, I want to be able to get the UTC value of this date.
EDIT2: Made my question simplier for clarification
I've got a UTC date, which I get from a webservice like: "/Date(1517216466000+0100)/" which is: Mon Jan 29 2018 10:01:06 GMT+0100 (W. Europe Standard Time) when printed to console.
I add a timezone to it with moment(this.inputDate).tz("Europe/Berlin").format(), but it stays 10:01:06, I guess because of my browsers GMT+1.
I want the ORIGINAL string to be used as a UTC date AND it should remain 10:01:06, not 09:01:06 as you can see above (2nd moment example), so with the timezone "Europe/Berlin" would be 11:01:6
In the .NET JSON formatted date "/Date(1517216466000+0100)/" the timezone offset can be ignored. It represents "2018-01-29T09:01:06.000Z", where the source system was at a timezone offset of +0100. So if you don't care about the source timezone, just ignore it.
It is also an identical moment in time to Mon Jan 29 2018 10:01:06 GMT+0100 (W. Europe Standard Time), just with a different offset.
UTC is not a format, it's a time standard. If you want to use ISO 8601 format:
Extract the first numeric value
Convert to Number
Pass to the Date constructor
Call the toISOString method on the resulting Date
var s = '/Date(-1517216466000+0100)/';
console.log(new Date(+s.replace(/^[^\d-]+(-?\d+).*$/,'$1')).toISOString());
You can also parse and format it using moment.js, which according to the documentation can handle the .NET JSON format without needing to specify the format. So you can either do that or extract the time value and parse it with the "x" format token:
var s = '/Date(1517216466000+0100)/';
// Let moment.js guess the format
console.log(moment(s).utc());
// Extract time value and supply format
console.log(moment(s.replace(/^[^\d-]+(-?\d+).*$/,'$1'), 'x').utc());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
"/Date(1517216466000+0100)/" is a non-standard way to serialise a date/time. Take a look at ISO8601 which defines several standard ways to represent dates and times.
That being said let's take a look at what this gets evaluated as...
moment("/Date(1517216466000+0100)/").toDate()
gives Mon Jan 29 2018 09:01:06 GMT+0000 (GMT Standard Time) for me (in the UK)
taking just the timestamp value 1517216466000
new Date(1517216466000)
also gives Mon Jan 29 2018 09:01:06 GMT+0000 (GMT Standard Time)
this means that the +0100 is being ignored.
You're not actually modifying the time so why would you expect it to save back as anything other than Mon Jan 29 2018 09:01:06
UPDATE
But the "original" string represents Mon Jan 29 2018 09:01:06 UTC the +0100 is ignored and it's just coincidence that your offset to UTC is also +0100. An off.
Offset and timezone are 2 different things. A timezone encompasses offset to UTC as well as when/if daylight savings comes in to force. Just because it says +0100 doesn't necessarily mean (W. Europe Standard Time) as it could just as easily be (West Africa Time) which is also UTC+0100 but doesn't observe daylight savings at all.
The time you have "/Date(1517216466000+0100)/" doesn't convey enough information to say which timezone it is and JS/moment just uses the timestamp 1517216466000 and as such uses UTC. When you console.log() this the browser writes it to screen as local time Mon Jan 29 2018 10:01:06 GMT+0100 (W. Europe Standard Time) but this is only a representation of the underlying datetime.
By telling moment to use a specific time zone, it's only changing how the date/time gets displayed and doesn't actually change the time it represents.
If you use a date picker to change the date/time then you'll have to serialise the value to send to the backend in an appropriate way that the .Net app you cannot change will understand and conveys what you intend.
Example
Get date from server as var serverTime = "/Date(1517216466000+0100)/"
convert this to a JS Date using moment var time = new moment(serverTime)
Let user specify TimeZone i.e. Japan Standard Time (UTC+9) time = time.tz("Japan")
time still represents Mon Jan 29 2018 09:01:06 UTC but when displayed on screen with time.format() gives "2018-01-29T18:01:06+09:00"
You stated "I want it to be 10 o'clock as UTC". Unfortunately the value you've is NOT 10 O'clock UTC and will never be 10 O'clock UTC because it isn't. It is 9 O'clock UTC
You could parse the value you get from the server yourself but the time from the server IS 9am UTC. If you change it to 10 am UTC then you would see that as Mon Jan 29 2018 11:01:06 GMT+0100 (W. Europe Standard Time) - 11 O'clock local time.
Thanks everyone for your detailed answers, they helped me a lot in understanding my problem! However, the right solution was the following:
10 o'clock was a UTC time in the database and it got interpreted as 9 o'clock UTC in Moment.js because C# handled it as a local time. So, before sending the date to the client, I had to indicate that its a UTC:
var utcToClient = DateTime.SpecifyKind(downtime.DownTimeStartUTC, DateTimeKind.Utc)
Then, in Moment I could create a UTC with:
var jsUtc = moment.utc(downtime.DownTimeStartUTC)
Changing the timezones was a breeze with:
jsUtc.tz(userSelectedTimezone)
And saving the date in the database, I used this in C#:
var utcFromClient = Record.DownTimeStartUTC.ToUniversalTime()
I'm sending the server time object with zero date, the sent date is:
Thu Jan 01 1970 01:02:01 GMT+0200
How can I convert it to GMT+0000? I need to tell the server about some task duration, so I want it to be just 01:02:01 as a duration. But the sent date is local and the server understands it as 03:02:01! How can I zero the GMT index?
Thanks
Getting the GMT time out of a JavaScript Date object is simple enough -
Date.prototype.toUTCString()
The toUTCString() method converts a date to a string, using the UTC time zone.
For example:
var test = new Date('Thu Jan 01 1970 01:02:01 GMT+0200').toUTCString();
console.log(test);
Note that this correctly outputs Wed, 31 Dec 1969 23:02:01 GMT, which although it not what you are looking for, is converting the provided Date to GMT.
To get what you want out of your input, a regular expression is useful. Caveats:
assumes duration will never be more than 23 hours, 59 minutes, and 59 seconds. If it is this will break.
var test = 'Thu Jan 01 1970 01:02:01 GMT+0200';
var durationMatcher = /\d\d:\d\d:\d\d/;
console.log(test.match(durationMatcher));
If you can, consider working in some values that works for you with one number - number of milliseconds for example.
function convertToGmt(pdate)
{
var newDate = new Date(pdate);
return (newDate.getUTCHours()<10?"0"+newDate.getUTCHours():newDate.getUTCHours())+":"+(newDate.getUTCMinutes()<10?"0"+newDate.getUTCMinutes():newDate.getUTCMinutes())+":"+(newDate.getUTCSeconds()<10?"0"+newDate.getUTCSeconds():newDate.getUTCSeconds());
}
Now use this function and call it by passing you date.
Notice that getUTCHours() returns correct hour in UTC.
Working Fiddle
I've a JavaScript date string which I want to convert in understandable form. E.g.
new Date(1415988000000)
which gives the output
Fri Nov 14 2014 23:30:00 GMT+0530 (IST)
I want to do this in python. I don't want to use PyV8. Is there any other alternative ?
You don't need to write Javascript in Python to convert the timestamps.
The following works in pure python.
Note:
The JS Date format is the number of milliseconds since Jan 1,1970 UTC.
Oddly enough, the python format for time.time() is the number of seconds since Jan 1, 1970 UTC.
This standard is sometimes called Unix time
First, for the milliseconds to seconds time conversion, you need to divide 1415988000000 by 1000, obtaining 1415988000
Then you can use the datetime library like this:
import datetime
d = datetime.datetime.fromtimestamp(1415988000)
print d
Obtaining:
2014-11-14 13:00:00
This print seems to have converted d to my TZ which is UTC-5.
So UTC time would be 18:00.
This explains the later time, 23:30, you receive in JS for the same stamp in IST or UTC+5:30