I need to convert session.lastAccessedTime object from jsp into Javascript Date object. Currently, it displays as long object. How can I convert to Javascript date object?
console.log('MaxInactive Interval == ' + ${pageContext.session.lastAccessedTime});
You can use Date.parse() method to convert date string to date object.
MDN Date.parse()
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
Date.parse(lastAccessedTime)
Related
const date = new Date();
date.setDate(date.getDate() - 30);
console.log(date); // 2018-03-03T23:10:24.063Z
console.log(date + 'hello'); // Sat Mar 03 2018 15:10:59 GMT-0800 (PST)hello
What's going on here? How can I use the date value without formatting it to be human readable? Thanks!
2018-03-03T23:10:24.063Z
This is date.toISOString(), so date.toISOString() + 'hello'.
toJSON() is your friend (more often than not):
const date = new Date();
date.setDate(date.getDate() - 30);
console.log(date);
console.log(date.toString());
console.log(`${date.toJSON()}hello`);
Internally, Date.prototype.toJSON() uses Date.prototype.toISOString().
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toJSON
When concatenating a date object with a string, internally Date.prototype.toString() is being called - and that creates the output you do not want in your case.
The Date object overrides the toString() method of the Object object; it does not inherit Object.prototype.toString(). For Date objects, the toString() method returns a string representation of the object.
The toString() method always returns a string representation of the date in American English.
JavaScript calls the toString() method automatically when a date is to be represented as a text value or when a date is referred to in a string concatenation.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString
I want to get the UTC time using moment.utc() as a date object instead of epoch or string (moment.utc().format() or using .toISOString()). moment.utc().toDate() returns my local time. Any help would be appreciated.
You can use moment().toDate(). If you coerce the date to a string (e.g. by sending it to the console, alert, etc.), the built-in (implementation dependent) toString method will typically use the host timezone settings to generate a string, e.g.
var m = moment().toDate(); // Equivalent to new Date()
console.log(m + '') // coerce to string uses built-in toString
console.log(m.toISOString()) // ISO 8601 string offset +0000
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
How can we convert datetime string to UTC in javascript. We are getting following JSON from REST service
[
{
"CreationTime":"June 2, 2015 8:04:53 PM IST",
"category":"UI",
"severity":"MAJOR",
"source":"BILLING",
"status":"ASSIGNED"
}
]
we are able to get CreationTime into a String variable but unable to convert to UTC. Any idea to convert this?
Using toUTCString():
var toUTC = new Date("June 2, 2015 8:04:53").toUTCString()
In Javascript you can use this method to convert a date from a Date() object, but not from a IST string. So you need format this string to a Date() object , then you can convert it to UTC. In this topic says what I mean.
Note If you try June 2, 2015 8:04:53 PM IST JavasScript take it as invalid date, for that you have to use .replace() function to remove the IST part of the string.
I want to take date in my module with JSON format and when I am converting my date value to json then it changes the timezone ultimately date gets change for eg
var myDateWithJson=(new Date(2014, 03, 11).toJSON());
alert("Date With Json " +myDateWithJson);
var myDateWithoutJson = new Date(2014,03,11);
alert("Date Without Json " + myDateWithoutJson);
I also gone through covert json without timezone but, I don't think that is better approch
Please guide me for the better approch
In your code:
var myDateWithJson=(new Date(2014, 03, 11).toJSON());
will create a date object for 00:00:00 on the morning of 11 April 2014 (note that months are zero indexed here) in your current locale based on system settings. Calling toJSON returns an ISO 8601 date and time string for the equivalent moment based on UTC.
The date object will have an internal time value that is milliseconds since 1970-01-01T00:00:00Z.
var myDateWithoutJson = new Date(2014,03,11);
That creates a date object for exactly the same moment in time, i.e. with exactly the same time value.
alert("Date Without Json " + myDateWithoutJson);
That calls the toString method of the date object that returns a human readable string representing the date and time in the current locale based on system settings.
So the first is a UTC string, the second is a local string. Both represent the exact same instant in time, and, if converted back to Date objects, will have exactly the same internal time value.
Is it possible in javascript to convert some date in timestamp ?
i have date in this format 2010-03-09 12:21:00 and i want to convert it into its equivalent time stamp with javascript.
In response to your edit:
You need to parse the date string to build a Date object, and then you can get the timestamp, for example:
function getTimestamp(str) {
var d = str.match(/\d+/g); // extract date parts
return +new Date(d[0], d[1] - 1, d[2], d[3], d[4], d[5]); // build Date object
}
getTimestamp("2010-03-09 12:21:00"); // 1268158860000
In the above function I use a simple regular expression to extract the digits, then I build a new Date object using the Date constructor with that parts (Note: The Date object handles months as 0 based numbers, e.g. 0-Jan, 1-Feb, ..., 11-Dec).
Then I use the unary plus operator to get the timestamp.
Note also that the timestamp is expressed in milliseconds.
+(new Date())
Does the job.
The getTime() method of Date object instances returns the number of milliseconds since the epoch; that's a pretty good timestamp.