1370131200000
I was using hichart to plot timeline data. in the example the date field in x axis is like this. But my timestamp values are like 1502582400.
It's UNIX time stamp (Wikipedia)
1502582400 is 08/13/2017 # 12:00am (UTC)
You can find how to convert to other formats here.
JavaScript dates are numbers which specify the number of milliseconds since January 1, 1970, 00:00:00.
Related
I am getting timestamps for estimated bus arrival times from an API as a timestamp / epoch: 1536589019000. If I go to a website like this I get the appropriate format:
Monday, September 10, 2018 7:16:59 AM
But if I attempt to convert the date in javascript, for example, with momentjs, I get some date far into the future: 50662-08-08 08:03
moment.unix(estimatedArrivalTime).format("YYYY-MM-DD HH:mm")
How do I convert a unix timestamp properly?
divide the time by thousand , moment.unix() expects time to be in seconds
moment.unix(1536589019).format("YYYY-MM-DD HH:mm")
You're getting a timestamp in millesconds instead of seconds. Just passing it like so should work: moment(1536589019000).format("YYYY-MM-DD HH:mm")
how can I convert "/Date(1479250800000)/" String to a C# Datetime?
Thanks in advance
Assuming the value inside the brackets is number of ticks:
var datstr = "/Date(1479250800000)/";
long ticks = Convert.ToInt64(datstr.Substring(6, 13));
DateTime date = new DateTime(ticks);
There will be a difference between .Net and javascript tick value:
The JavaScript Date type's origin is the Unix epoch: midnight on 1 January 1970. The .NET DateTime type's origin is midnight on 1 January 0001. If by "ticks" you mean something like "milliseconds since the epoch", you can call ".getTime()
There are 621355968000000000 epoch ticks for javascript from Ist Jan 1900 to Ist Jan 1970. And here 10000 are the ticks per milliseconds.
quoted from here. You will need to correct for this. the last line would look like the following:
DateTime date = new DateTime(ticks * 10000 + 621355968000000000);
You'll get the string "/Date(1479250800000)/" as a date value in javascript if you are sending it from C#. But if you sending back the value to a date field in C# then the value will be deserialized as DateTime value. You don't need to convert it explicitly, just get it in a DateTime field.
Need help to convert exactly from ISO Date string to Date:
I have an ISO string date: "2016-01-23T22:23:32.927".
But when I use new Date(dateString) to convert Date, the result is wrong:
var date = new Date("2016-01-23T22:23:32.927");
The result is: Sun Jan 24 2016 05:23:32 GMT+0700. It's not true. I want the date is 23 not 24.
Please help me. Thanks a lot!
You need to supply a timezone offset with your iso date. Since there isn't one, it assumes the date to be in GMT and when you log it out, it prints it in the timezone of your browser. I think that if you pass "2016-01-23T22:23:32.927+07:00" to new Date() you would get the value you are expecting.
JavaScript environments (browser, node,...) use a single timezone for formatting dates as strings. Usually this is your system's timezone. Based on the output you get, yours is GMT+0700.
So what happened:
The string you passed as ISO format to the Date constructor doesn't specify a timezone. In this case it is treated as UTC.
When you then output the date (I'll assume with console.log), it is converted to the timezone of your environment. In this case 7 hours where added.
If that doesn't suit you, you can change the way you output the date. This depends on what output you want, e.g.:
If you just want the UTC timezone again, you can use date.toISOString().
If you want to output it in another timezone, you can call date.getTimezoneOffset() and figure out the difference between both timezones. You'd then probably need to get the individual date parts and add/subtract the timezone difference accordingly. At this point you could consider using an existing library, taking into account their possible disadvantages.
If you're willing and able to add a dependency, I recommend using moment.js for this. It makes date handling in Javascript much more straightforward and a lot safer, and fixes your specific problem right out of the box.
To do this, 1st load it from a CDN, e.g. Moment.JS 2.14.1 minified. Then use it as follows:
var date = moment("2016-01-23T22:23:32.927");
console.log(date);
// output: Sat Jan 23 2016 22:23:32 GMT-0500
...i.e. your desired result :)
Here's a jsfiddle demonstrating this.
Use date.toUTCString()
it'll give you 23 instead of 24 as it Convert a date object to a string, according to universal time
I am using moment.js to convert a bunch of timestamps in it's specific timezone to a unix timestamp like this:
var timestamp = "2015-12-29T09:35:00.000-08:00";
console.log(moment("2015-12-29T09:35:00.000-08:00").unix();
console.log(moment("2015-12-29T09:35:00.000-08:00").tz("America/Los_Angeles").unix();
The console log of both the above statements is for some reason, the same - 1451361900. This unix timestamp which it is logging is in my local timezone and not the one I asked for: "America/Los_Angeles". What am I missing?
A unix timestamp, or Posix, should always be in the UTC (Coordinated Universal Time) format.
Moment is just doing something like
function unix () {
return Math.floor(+this / 1000);
}
Where it converts the date object to an integer and then converts from milliseconds to seconds.
The starting point is a regular javascript Date object, and the ECMA standard says
Date objects are based on a time value that is the number of
milliseconds since 1 January, 1970 UTC.
so date objects are always UTC when converted to the number of milliseconds since 1. January 1970 (epoch), i.e. you can't set another timezone on a Unix timestamp, both your dates are the same.
The proper way is to use moment-Timezone is this.
console.log(moment("2015-12-29T09:35:00").unix());
console.log(moment.tz("2015-12-29T09:35" , "America/Los_Angeles").unix());
In above your are providing time zone as a string too which is this last part ".000-08:00" and then you are providing another zone, which is incorrect.
As you are trying to find out the unix timestamp for the date "2015-12-29T09:35:00.000-08:00". In this date format timezone value is already present which is "-08:00", hence you get the same unix timestamp.
For getting the unix timestamp desired solution, remove the timezone value and use moment-timezone as :
console.log(moment.tz("2013-12-01", "America/Los_Angeles").unix());
For more details check moment-timezone
In my javascript the library Moment.js rounds my dates up.
Date: 2015-02-09T23:00:00.000Z
moment(Date).format('DD/MM'); ==> Becomes 10/02
I want 09/02 as result. Is there a possible way that the library not rounds the date?
The problem is likely one of timezones: by default, momentjs parses your string and converts it to your local timezone. If I see this correctly, the 'Z' in your date signifies zulu - or UTC time. If your timezone is +02:00 for example, that would make it the 10th, 01:00.
Use Moment#utc
moment(Date).utc().format('DD/MM');
to output format the date as UTC again.
Moment.js will output dates in the local time zone, so it might very well be that it's caused by a difference in timezones.
If you want to show the date/time as encoded into the original string, use parseZone like this:
var dateStr = "2015-02-09T23:00:00.000Z";
moment.parseZone(dateStr).format('DD/MM');
You can try like below.
moment(Date,['YYYY-MM-DD']).format('DD/MM');