I receive a date as a string of 18 numbers such (Example: "636664860000000000") from an API that uses .NET.
Based on my research, it is a windows SYSTEMTIME format value that needs to be converted to FILETIME then UTC. I am unable to find a way to do this other than through the back end itself.
Is there a way to convert this number to a UTC time stamp with JavaScript?
Give this a try: https://plnkr.co/edit/GXTO1gigMAbumRU643KB?p=preview
function convert(){
var winTicks = 10000000;
var uEpoch = 11644473600;
var time = document.getElementById('filetime').value;
var unixTime = time/winTicks - uEpoch;
console.log(unixTime);
var utc = new Date(unixTime * 1000).toUTCString();
var label = document.getElementById('utctime');
label.innerHTML = utc;
}
And compare with this tool: https://www.epochconverter.com/ldap
Related
Is there an equivalent for figuring out UTC offset by supplying date, time, time zone name using Angular? This is very easy in C# - example below:
TimeZoneInfo tzi = TimeZoneInfo.FindSystemTimeZoneById("US Eastern Standard Time");
string dateInput = "2022-11-15";
var parsedDate = DateTime.Parse(dateInput);
TimeSpan offset = tzi.GetUtcOffset(parsedDate); // this returns -5
string dateInput2 = "2022-03-20";
var parsedDate2 = DateTime.Parse(dateInput2);
TimeSpan offset2 = tzi.GetUtcOffset(parsedDate2); // this returns -4
The JavaScript getTimezoneOffset() method is used to find the timezone offset
let d = new Date(Date.parse("2022-03-21T06:38:30+0000"));
console.log(d.getTimezoneOffset()) // 120 minutes offset for me
After many hours spent on this I decided to give moment.js as suggested by Joosep.P to try some library and also by others. The below seems to work as expected. What is bizarre is if you try to use timezoneName other than where you currently are and if you debug, you will still see your own time zone name for the date, however the offset will be calculated correctly. I assume that this works because of proper setting .tz call.
var timezoneName = 'America/New_York';
var dateInput = '2022-11-15';
var parsedDate = moment.tz(dateInput, timezoneName);
var offseta = parsedDate.utcOffset()/60; // this returns -5
var offsetb = moment.tz(dateInput, timezoneName).format('Z'); // returns '-05:00' as a string
var dateInput2 = '2022-03-20';
var parsedDate2 = moment.tz(dateInput2, timezoneName);
var offset2a = parsedDate2.utcOffset()/60; // this returns -4
var offset2b = moment.tz(dateInput2, timezoneName).format('Z'); // returns '-04:00' as a string
I have this datetime stamp from Ruby:
2017-10-06 05:11:53 UTC
This datetime stamp from JavaScript:
"2017-10-07T12:07:06.694Z"
Is there a method to which I could find out the difference in seconds?
How do I convert from a ruby timestamp to a javascript timestamp?
You could do this using Ruby with strftime or directly in javascript as demonstrated by Tavish.
var myDate = new Date('2017-10-06 05:11:53 UTC')
How do I find the difference in seconds between two timestamps with Javascript?
var past = new Date('2017-10-06 05:11:53 UTC');
var future = new Date('2017-10-07T12:07:06.694Z');
var deltaInMilliseconds = future.getTime() - past.getTime();
var deltaInSeconds = deltaInMilliseconds / 1000;
console.log(deltaInSeconds)
There are lot of ways to do so. One way is:
var myDate = new Date('2017-10-06 05:11:53 UTC')
console.log(myDate)
var myDate1 = new Date('2017-10-07T12:07:06.694Z')
console.log(myDate1)
var difference = myDate.getSeconds() - myDate1.getSeconds()
console.log(`Seconds passed: ${difference}`)
Mozilla browser I have tried get my time-stamp in JavaScript like strtotime in php
My Code:
//var start_date = data.result[0].start_date;
var start_date = "2011-01-26 13:51:50";
var d = Date.parse(start_date) / 1000;
console.log(d);
// 1296030110
Above code is working fine in chrome. But not working in the Mozilla Browser. I am getting NaN value. Please help me.
After search in google I find one solution to add T between the date and time. so I have added. I am getting the output but the output is not the same in both browser.
var start_date = "2011-01-26T13:51:50";
var d = Date.parse(start_date) / 1000;
console.log(d);
//Mozilla = 1296030110
//Chrome = 1296044910
Do not parse strings with the Date constructor or Date.parse (they do the same thing), it is extremely unreliable, especially for non–standard strings (and some that are). To parse "2011-01-26 13:51:50" as a local time, use a library or a simple function like:
function parseDateTime(s) {
var b = s.split(/\D/);
return new Date(b[0],b[1]-1,b[2],b[3],b[4],b[5])
}
document.write(parseDateTime("2011-01-26 13:51:50") / 1000);
To include validation an support for missing values adds a bit more code on one more line.
var start_date = "2011-01-26 13:51:50";
var d = Date.now(start_date);
console.log(d);
it will run in mozila
you need not to perform any calculation
it automatically converts into milliseconds.
Try this. I am not sure this result is perfect or not.
var start_date = Date("2011-01-26 13:51:50");
var d = Date.parse(start_date) / 1000;
console.log(d);
//1454478429
Try this its work for all browser
start_date="2011-01-26 13:51:50".replace(" ","T");
start_date = new Date(start_date);
var d = start_date.getTime() / 1000;
this will work
var start_date = "Jan 26,2011 13:51:50 ";
var d = Date.parse(start_date)/1000;
console.log(d);
because
The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognised or contains illegal date values (e.g. 2015-02-31).
The parse() method takes a date string (such as "Dec 25, 1995") and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC.
I want time duration between two date time. I have the start date, start time, end date and end time. Now I have to find the difference between them.
Actually I have tried with this following code, but I got the alert like 'invalidate date'.
function myfunction()
{
var start_dt = '2013-10-29 10:10:00';
var end_dt = '2013-10-30 10:10:00';
var new_st_dt=new Date(start_dt);
var new_end_dt=new Date(end_dt);
alert('new_st_dt:'+new_st_dt);
alert('new_end_dt:'+new_end_dt);
var duration=new_end_dt - new_st_dt;
alert('duration:'+duration);
}
the alert msg like as follows:
new_st_dt:invalid date
new_end_dt: invalid date
duration:NaN
when I run in android simulator I got these alert messages.
Please help me how to get it? How to implement this?
You're passing an invalid ISO date string to that Date() constructor. It needs a form like
YYYY-MM-DDThh:mm:ss
for instance
2013-10-29T10:10:00
So you basically forgot the T to separate date and time. But even if the browser reads in the ISO string now, you would not have an unix timestamp to calculate with. You either can call
Date.parse( '2013-10-29T10:10:00' ); // returns a timestamp
or you need to explicitly parse the Date object, like
var duration=(+new_end_dt) - (+new_st_dt);
Further read: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
Try formatting you timestamps as isoformat so javascript recognizes them. (you put a "T" between the date and time). An example: '2013-10-29T10:10:00'
function dateDiff(){
var start_dt = '2013-10-29 10:10:00';
var end_dt = '2013-10-30 10:10:00';
var d1= start_dt ;
d1.split("-");
var d2= end_dt ;
d2.split("-");
var t1 = new Date(d2[0],d2[1],d2[2]);
var t2 = new Date(d1[0],d1[1],d1[2]);
var dif = t1.getTime() - t2.getTime();
var Seconds_from_T1_to_T2 = dif / 1000;
return Math.abs(Seconds_from_T1_to_T2);
}
I have a page that is currently using the datetime microformat to display a timestamp, but I have only been showing the human-readable time for my own time zone:
<abbr class="published" title="2009-01-09T09:16:00-05:00">
Friday, January 9, 2009 at 9:16 am (EST)</abbr>
What I'd like to do is rewrite the innerHTML for the abbr tag to be the same format, but in the user's local timezone. So for a reader in Seattle, the above should be converted to:
<abbr class="published" title="2009-01-09T09:16:00-05:00">
Friday, January 9, 2009 at 6:16 am (PST)</abbr>
I've looked at the Javascript Date object, which allows me to get the local timezone offset. But I have a few problems:
I don't see an easy way to create a new Date object from an ISO-8601 timestamp. (I suppose I could parse with substrings or regex if there's no faster way.)
I don't see a way to get the named abbreviation for the timezone. For example, for a reader in Seattle, I'd want the time to have "(PST)" appended to the end, otherwise it is not clear to that user that the timestamp has been converted (especially if he is a frequent visitor and has become accustomed to the fact that my times are in EST).
Here is code of mine that parses an ISO timestamp:
function isoDateStringToDate (datestr) {
if (! this.re) {
// The date in YYYY-MM-DD or YYYYMMDD format
var datere = "(\\d{4})-?(\\d{2})-?(\\d{2})";
// The time in HH:MM:SS[.uuuu] or HHMMSS[.uuuu] format
var timere = "(\\d{2}):?(\\d{2}):?(\\d{2}(?:\\.\\d+)?)";
// The timezone as Z or in +HH[:MM] or -HH[:MM] format
var tzre = "(Z|(?:\\+|-)\\d{2}(?:\\:\\d{2})?)?";
this.re = new RegExp("^" + datere + "[ T]" + timere + tzre + "$");
}
var matches = this.re.exec(datestr);
if (! matches)
return null;
var year = matches[1];
var month = matches[2] - 1;
var day = matches[3];
var hour = matches[4];
var minute = matches[5];
var second = Math.floor(matches[6]);
var ms = matches[6] - second;
var tz = matches[7];
var ms = 0;
var offset = 0;
if (tz && tz != "Z") {
var tzmatches = tz.match(/^(\+|-)(\d{2})(\:(\d{2}))$/);
if (tzmatches) {
offset = Number(tzmatches[2]) * 60 + Number(tzmatches[4]);
if (tzmatches[1] == "-")
offset = -offset;
}
}
offset *= 60 * 1000;
var dateval = Date.UTC(year, month, day, hour, minute, second, ms) - offset;
return new Date(dateval);
}
Unfortunately, it doesn't handle timezone abbreviations either. You would have to modify the "tzre" expression to accept letters, and the only solution I know of to deal with timezone abbreviations in Javascript is to have a look-up table which you keep up to date manually in the event of changes to regional daylight savings times.
EcmaScript formalized the addition of an ISO-8601 style string as an imput for a JavaScript date. Since most JS implementations don't support this, I created a wrapper to the Date object, that has this functionality. If you set the title tags to output in UTC/GMT/Z/Zulu offset, you can use my EcmaScript 5 extensions for JS's Date object.
For DateTime values that are to be used in client-side scripts, I generally try to always do the following. Store date+time in UTC zone (even in databases). Transmit date-times in UTC zone. From client to server, you can use the .toISOString() method in the above link. From server-to client this is relatively easy.
Via jQuery (with extension):
$('.published').each(function(){
var dtm = new Date(this.title);
if (!isNaN(dtm)) {
this.text(dtm.toString());
}
});
I don't recall if I added support for non-utc date-times in the input, but wouldn't be too hard to account for them.