So I've got two dates I'd like to get the difference of:
console.log(new Date(Date.now()).toISOString()); //2017-07-07T16:55:30.471Z
console.log(asset.past[i].date); //2017-07-06T20:29:00.670Z
var a = moment([new Date(Date.now()).toISOString()]);
console.log(a); //Moment Date:Sun Jan 01 2017 00:00:00 GMT+0000
var b = moment([asset.past[i].date]);
console.log(b); //Moment Date:Sun Jan 01 2017 00:00:00 GMT+0000
console.log(a.diff(b, 'seconds', true)); //0
console.log(a.diff(b, 'days', true)); //0
console.log(a.diff(b, 'months', true)); //0
I've put the out put of the console logs as comments afterwards. I assume it doesn't recognize the date format as ISO 8601 and defaults to Sun Jan 01 2017 00:00:00 GMT+0000. Either way, any idea how to fix it?
Cheers, Ed.
Why are you passing in an array to the moment constructor? What do you want a to actually be, just the current date? If so just do moment(). If you want to pass in a string do it like you correctly did on line 1.
Here are just the 2 fixes where I removed the square brackets. Again, you can just do moment() to get a moment that points to now.
// var a = moment(new Date(Date.now()).toISOString());
var a = moment();
var b = moment(asset.past[i].date);
Related
var now = moment(1609891200000, "x").format('MMM DD h:mm A');
var x = new Date(1609891200000);
console.log(now); // Prints Jan 06 12:00 AM
console.log(x.toLocaleTimeString()); // Prints 05:30:00
I don't know why I keep getting 5:30 as time. I need a way to get the correct time i.e 12:00 AM without the use of Moment.
How can I do this?
You should provide a code of time zone,
If there is no code, then it will provide by default
var date = new Date(Date.UTC(2012, 11, 12, 3, 0, 0));
// toLocaleTimeString() without arguments depends on the implementation,
// the default locale, and the default time zone
console.log(date.toLocaleTimeString());
// → "7:00:00 PM" if run in en-US locale with time zone America/Los_Angeles
Instead, you can use
var x = new Date(1609891200000);
x.toGMTString() // "Wed, 06 Jan 2021 00:00:00 GMT"
x.toUTCString() // "Wed, 06 Jan 2021 00:00:00 GMT"
x.toISOString() // "2021-01-06T00:00:00.000Z"
I am trying to convert a date in format momentjs into a date from javascript native new Date().
The problem is that if I have moment(myDay).toDate(); it converts to the current date, and I want the date from myDay.
myDay looks like: "YYYY-MM-DD" => 2017-11-24 and I would like to have it with the format: Fri Nov 24 2017 20:17:11 GMT+0100 (Hora estándar romance) but I get Thu Nov 16 2017 etc...
It is possible to convert it like that way?
Don't need moment:
let [yr, mn, day] = myDay.split('-').map(Number);
// note that JS months are 0-11 not 1-12
let datestr = new Date(yr, mn - 1, dy).toString();
console.log(datestr); // "Fri Nov 24 2017 00:00:00 GMT-0500 (EST)"
you want something like this:
moment(myDay, "YYYY-MM-DD").toString();
moment().toString() Returns an english string in a similar format to JS Date's .toString().
moment().toString() // "Sat Apr 30 2016 16:59:46 GMT-0500"
I would like to compare the given date in the below format in JaveScript. I have tried the following,
Thu May 19 2016 00:00:00 GMT+0530 (India Standard Time)
Thu May 20 2016 00:00:00 GMT+0530 (India Standard Time)
var ExpiryDate = userAccount.ExpiryDate();
var datetoday = new Date();
var Expired = (DateTime.Compare(ExpiryDate, datetoday) == -1 ) ? true : false;
//if expiry date is less than today date then var expired should be true
But didn't worked. I could not compare those two dates. It results in un handled exception. Is there any other way to do this date comparison in JaveScript ?
I have referred the following answers in SO but they are in different date format. So that I have raised this question,
javascript compare two dates and throw an alert
Javascript comparing two dates has wrong result
Compare two dates in JavaScript
Javascript compare two dates to get a difference
Any suggestion would be helpful.
var date = new Date();
//# => Fri May 20 2016 16:09:43 GMT+0530 (India Standard Time)
var date2 = new Date();
date2.setDate(date.getDate() - 1);
//# => Thu May 19 2016 16:09:43 GMT+0530 (India Standard Time)
date > date2 //# => true
use getTime()
var date1 = (new Date("20 May 2016")).getTime();
var date2 = (new Date("19 May 2016")).getTime();
date1>date2
You will find some good method here
I am trying to set "formattedLocalTime" to the Pacific time and my 4 lines of code look as below.
Though the chrome debugger displays "locTime" as "Tue Sep 30 2014 16:17:25" which is the correct value I expect, the formattedLocalTime in the last line is "09/30/2014 11:17 pm" which is UTC time and not the timezone I have set (America/Los_Angeles) which should be "09/30/2014 4:17 pm" (4:17 instead of 11:17)
Would highly appreciate any suggestions.
var timestamp = 1412144245453; // Tue Sep 30 2014 23:17:25
var utc = moment.tz(timestamp, "Etc/UTC"); // Tue Sep 30 2014 23:17:25 (displayed in chrome debugger)
var locTime = utc.clone().tz("America/Los_Angeles"); // Tue Sep 30 2014 16:17:25 (displayed in chrome debugger)
var formattedLocalTime = moment(locTime).format("MM/DD/YYYY h:mm a")
You can do this in one step:
moment.tz(1412144245453, 'America/Los_Angeles').format('MM/DD/YYYY h:mm a')
OUTPUT: "09/30/2014 11:17 pm"
Also, you had evaluated the times for this timestamp incorrectly. In UTC, this timestamp is October 1st, 2014 6:17:25 AM. The corresponding Pacific time is indeed September 30th, 2014, 11:17:25 PM.
You can check this using a site like epochconverter.com, or in moment.js like so:
moment.utc(1412144245453).format() // "2014-10-01T06:17:25+00:00"
try to use:
var formattedLocalTime = locTime.format("MM/DD/YYYY h:mm a")
if you write moment(locTime) then your datetime will be converted back to local time
Use: moment-timezone - TypeError: moment().tz is not a function
const moment = require('moment-timezone');
const time = moment.tz(1412144245453, 'America/Los_Angeles').format('MM/DD/YYYY h:mm a');
console.log("time : ", time);
Output: time : 09/30/2014 11:17 pm
I found and inconsistent result when using the JavaScript date.getMonth() and date.getUTCMonth(), but only with some dates. The following example demonstrates the problem:
<!DOCTYPE html>
<html>
<body onload="myFunction()">
<p id="demo">Click the button to display the month</p>
<script type="text/javascript">
function myFunction()
{
var d = new Date(2012, 8, 1);
var x = document.getElementById("demo");
x.innerHTML=d;
x.innerHTML+='<br/>result: ' + d.getMonth();
x.innerHTML+='<br/>result UTC: ' + d.getUTCMonth();
}
</script>
</body>
</html>
The output of this example is:
Sat Sep 01 2012 00:00:00 GMT+0100 (Hora de Verão de GMT)
result: 8
result UTC: 7
If i change the date to (2012, 2, 1) the output is:
Thu Mar 01 2012 00:00:00 GMT+0000 (Hora padrão de GMT)
result: 2
result UTC: 2
In the first example, getMonth returns 7 and getUTCMonth returns 8. In the second example, both returns the same value 2.
Does anyone already experiences this situation? I am from Portugal and i think that it has something to be with my GMT but i don't understand why this is happening, because the examples are running in same circumstances.
Thanks in advances
You will see that, depending on YOUR TIMEZONE, the console logs may be different. I chose the first of the month '01' because it will be given a midnight default time '00:00:00', which will result in some timezones yielding February instead of March (you can get the full scoop here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) :
let date1 = '2019-03-01'; // defaults to UTC
let date2 = '2019-03-01T14:48:00'; // LOCAL date/time
let dt1 = new Date(date1);
let dt2 = new Date(date2)
let month1 = dt1.getMonth();
let month2 = dt2.getMonth();
console.log("mon1: " + month1);
console.log("mon2: " + month2);
You will find that it is caused by DST difference.
Universal Time Zone date methods are used for working with UTC dates
Date returns month between 0 to 11
new Date(1976, 01 , 18) -
Wed Feb 18 1976 00:00:00 GMT+0530 (India Standard Time)
*getUTCDate return same as getDate() but returns Date based on World Time Zone, same with month and year
new Date(1976, 01 , 18).getUTCDate() -
17
new Date(1976, 01 , 18).getDate() -
18
new Date(1976, 02 , 18).getUTCMonth() -
2
new Date(1976, 01 , 18).getMonth() -
1
new Date(1976, 01 , 18).getYear() -
76
new Date(1976, 01 , 18).getUTCFullYear() -
1976
new Date(1976, 01 , 18).getFullYear() -
1976
A Date in js is just a timestamp, meaning there is no timezone information in any Date instance. A date is an absolute timed event (in opposition to a wall clock time which is relative to your timezone).
So… when you print the date to the console, because there is no timezone information in the date object, it will use your browser's timezone to format the date.
This is embarrassing because if you provide the same date to 2 clients, one in US and the other one in EU and ask them the date's month, because both are using their own timezone, you might end up with different answers.
To prevent this, getUTCMonth(); will use a default timezone of UTC (+0) instead of the client's browser so that the answer will be consistent whatever the client's timezone.