Hello All, I have time in milliseconds and I want convert it into "dd/mm/yyyy hh:mm:ss"
I have tried the following code in javascript but I failed to do it, What I get is "Thu Jul 07 2016 16:22:10 GMT+0530 (IST)" this full timestamp, Code I tried is as follows:
self.users[i].lastLoggedIn = (new Date(self.users[i].lastLoggedIn)).toString("mm/dd/yyyy hh:mm:ss");
Where am I going wrong? for reference I have attached one screen shot of the same. Thank you..!!!
I suggest using moment.js for handling dates in Javascript. It is available for browsers, server-side engines and so on.
moment(self.users[i].lastLoggedIn).format("mm/dd/yyyy hh:mm:ss")
Related
I've read many posts on this topic, still i have some questions
first i get an date input (28/09/2019) from html input tag, let's call it aDate
<input name="ExpireDate" ng-model="personalDetail.ExpireDate" type="date">
in Javascript
console.log(aDate) gives Sat Sep 28 2019 00:00:00 GMT+1000 (Australian Eastern Standard Time)
console.log(aDate.toISOString()) gives 2019-09-27T14:00:00.000Z
i know they are both correct as the first one is UTC+10 time and the second one is UTC+0 time
and then i pass aDate to a spring boot application, it shows 2019-09-27T14:00:00.000Z if i parse it to LocalDate(which is supported by postgresql) it will lose T14:00:00.000Z part
then if i try to convert LocalDate 2019-09-27 in Java to Date in Javascript the date is now one day off.
my current solution is use LocalDateTime instead of LocalDate, it worked but i really dont want to store that time info in database
is there a way to get rid of the time info in Javascript Date? or any other solutions to tackle this
another strange things is why i specified type="date" in html input tag, it still give me datetime data?
UPDATE:
LocalDateTime does not work
converting 2019-09-27T14:00:00.000Z to LocalDateTime yields 2019-09-27T14:00
but in Javascript new Date('2019-09-27T14:00') treats it as local time and yields
"Fri Sep 27 2019 14:00:00 GMT+1000 (Australian Eastern Standard Time)"
"2019-09-27T04:00:00.000Z"
I am trying to use luxon to generate a new date using a timezone. This is my code:
var luxon = require('luxon');
luxon.Settings.defaultZoneName = 'UTC+4';
var date = luxon.DateTime.local();
console.log(date);
var now = new Date(date.ts);
console.log(now.toString());
And this is the console:
DateTime {
ts: 2018-09-13T13:09:45.333+04:00,
zone: UTC+4,
locale: en-US }
Thu Sep 13 2018 11:09:45 GMT+0200 (CEST)
But if I try to access the ts property like so
var date = luxon.DateTime.local();
console.log(date.ts); // here
var now = new Date(date.ts);
console.log(now.toString());
I get this in the console:
1536830052009
Thu Sep 13 2018 11:14:12 GMT+0200 (CEST)
Why is that? Is it doing some kind of math in the background? Also it turns out this date.ts is just ignoring my timezone. How can I fix that?
First 1536830052009, This is your time in milliseconds,
new Date(1536830052009)
// output Thu Sep 13 2018 11:14:12 GMT+0200 (CEST)
You may want to check your timezone with getTimezoneOffset()
Returns the time difference between UTC time and local time, in minutes
Many people use moment.js to play with Date, I know it is not in your question but maybe you could find some usefull things
ts is not a public property and you shouldn't use it. Luxon does all sorts of tricks under the covers to get the math right. If you want the timestamp, just use date.toMillis(). If you want a JS Date, use date.toJSDate().
Two other important things to know:
It's not ignoring your zone. The zone doesn't change the time. It's more like metadata about a time that affects how we display it. The Luxon docs cover this a bit. You shouldn't expect to extract a different timestamp by fiddling with the zone. Now is always now.
Remember that the native Date object doesn't support timezones other than your local one. So anytime you convert from a Luxon object to a native Date, that information is lost. The time itself will be the same (meaning, it will represent the same millisecond), but it will express it in the local time.
I using django model forms to submit data to the database.
I use JavaScript to auto-fill the form with the following
document.getElementById('id_date_received').value = Date();
This outputs: Mon Feb 06 2017 11:39:05 GMT+0000 (GMT)
while django's models.DateTimeField expects: 2017-02-06 11:39
How do i convert: Mon Feb 06 2017 11:39:05 GMT+0000 (GMT) to 2017-02-06 11:39
Thanks
IMO, the best solution would be using unix timestamps, because you can avoid all complex stuff connected with timezones and time parsing.
JS:
js_date = new Date('2012.08.10');
// getTime() returns milliseconds from the UNIX epoch,
// so divide it by 1000 to get the seconds representation.
js_timestamp = js_date.getTime() / 1000;
Python:
python_date = datetime.datetime.fromtimestamp(js_timestamp)
You should consider use Moment.js, it's the easiest javascript library to manipulate dates and timezone formats.
So the code would by something like this:
moment(YOUR_DATE_VARIABLE).format('YYYY-MM-DD HH:mm'); // 2017-02-06 11:39
Hope this help you.
You can set the pattern of date in your model form that accept particular format.
input_formats=[list of datetime patterns that you want to accept]
This is a bit of a long-winded solution but this should work to convert Date to django date time
I first convert the Date to a string by cast
(String(date_var))
then when I receive the API call I convert it using this command
datetime.datetime.strptime(",".join(original_time[:original_time.find("(")-1].split(" ")).replace("GMT",""), '%a,%B,%d,%Y,%H:%M:%S,%z')
I would recommend preserving the timezone as different servers can be in different timezones which can screw up your dates!
I am a newbie in javascript, come to my question.
I am using ionic 2 build application in which i am using date time picker for taking a time.
I am getting a a time format is in the "hh:mm" using time picker. eg(10:11) which is in string format and i am using Date() function which give me date is in something like
"Mon Aug 01 2016 01:32:03 GMT-0700 (Pacific Daylight Time)"
I want to replace "hh:mm"(01:32) from Date object with my string output of "hh:mm"(10:11) and then want to convert that into new Date object.
I also tried split and slice function but doesn't work.
If i get some guide about this will be very helpful.
I will be very helpful to all of you.
thanks
First of all, read up on Date.
In your case, the following code is a starting point.
var d = new Date();
d.setHours('10');
d.setMinutes('11');
Of course, you should exchange '10' and '11' with your picker data. Also, there are many other methods you can use on the Date object.
I have a function that gets the date in the DATETIME format:
2015-06-18 00:00:00
Doing moment.utc("2015-06-18 00:00:00").toDate() will display different results in Firefox and Chrome:
Firefox: Date 2015-06-18T00:00:00.000Z
Chrome: Thu Jun 18 2015 03:00:00 GMT+0300 (EEST)
Also, using new Date("2015-06-18 00:00:00") will return Invalid Date in Firefox, but adding a "T" before the hours will fix that issue. But then if I do new Date("2015-06-18T00:00:00") will return:
Firefox: Date 2015-06-17T21:00:00.000Z
Chrome: Thu Jun 18 2015 03:00:00 GMT+0300 (EEST)
It's driving me nuts.
How can I get both browsers to show the same hour?
How can I get Firefox to display the result in Chrome's format?
The whole point of using a dedicated date library is to obtain transparent cross-browser date features. However, as soon as you run .toDate() you get back the native Date object. If you then convert it to string by using the builtin Date.toString() method you've finally dropped all the library goodies and got back to vanilla JavaScript.
Tips:
Don't use strings expect for display purposes
Use the library features to generate those strings
try
new Date("2015-06-18T00:00:00").toString()
looks the same on both for me