Date is in string format - javascript

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.

Related

Problems trying to get the correct date with Date() and moment.js

I need to convert a date initially given as a string in the format "dd/mm/yyyy" to a valid date object but I'm running into problems. I use the moment.js library for this but when I try to convert it to a date object, it treats it internally incorrectly.
Initially I have:
var date_text = '02/01/2020 00:10'; //i.e. January 2, 2020.
var initial_date = new Date(date_text); //here js takes its default format and the problem starts.
var a = moment(initial_date,'DD/MM/YYYY');
console.log(a); //it keeps telling me that the date is February 1, 2020.
I have seen that this is often done "manually", i.e. by changing the order of the month and day. However, I find it hard to believe that a library as comprehensive and powerfull as moment.js has no way of doing this. I guess I haven't figured out how to do it.
Please, can someone help me in this regard?
What I specifically need is to pick up the date correctly (January 2nd and not February 1st) and preferably do it without having to alter the date "manually", that is, doing it only with the Date object of js and moment.js.
Thank you very much.
You don't really need initial_date to format the date_text when you are using moment.js Try the below code to fix the date parse issue
var date_text = '02/01/2020 00:10'; //i.e. January 2, 2020.
var a = moment(date_text, 'DD-MM-YYYY HH:mm');
console.log(a); // Thu Jan 02 2020 00:10:00 GMT+0530
Then format the date in your desired format
var b = a.format('DD/MM/YYYY');
console.log(b); // 02/01/2020

Convert JavaScript date() to Python Django models.DateTimeField

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!

Json stringify changes dates

Hello I have an object in js with a field date. I try to stringify it at an ajax request but the result is inconsistent. After stringify the new object is one day earlier.
To be more specific this is the code on my file:
console.log(reservation.checkin);
console.log( JSON.stringify(reservation.checkin));
And this is the outcome:
Thu Jan 01 2015 00:00:00 GMT+0200 (EET)
"2014-12-31T22:00:00.000Z"
Am I doing something wrong? Is that output what it should be? Thx in advance!
edit: From an answer below it seems that it is at different timezone. What is the correct way to stringify this date?
It's not changing the date, just showing it in another timezone (UTC / GMT)
GMT+0200 (EET) means 2 hours differenece with UTC / GMT
so that's exaclty what you see in the result.
it depends a bit on the purpose. If you want ot post this in another API, it should work fine (presuming, the api uses timezone standards), in case you want to just show it in a gui... why use the json stringify...
I am not going to do all the math for you, i suggest you know why now, so just google: 'javascript format timezone' or something like that.
e.g:
Convert date to another timezone in JavaScript
Well, it is timezone - your date is GMT +2:00,
and after applying Stringify you get UTC.
You may want to check Date method .getTimezoneOffset() and maybe update the date
JSON.stringify is calling Date.toJSON() to convert the date.
See The "right" JSON date format.

How do I convert to browser timezones using phstc dateFormat?

EDIT : Btw, I have no idea why this question was marked as a duplicate. The answers in the original question does not work for me. i.e, getting wrong results and stuffs. Furthermore, none of the answers deal with phstc's dateFormat function. Do correct me if I'm wrong. Btw, I have solved this question. Do take a look at my answer.
I want to change a UTC datetime to my browser's timezone. I'm using phstc's dateFormat in pure javascript form. Let's say I convert a datetime of 2014-06-27 07:11:16 using a javascript Date() function. The result I got was
Fri Jun 27 2014 07:11:16 GMT+0800 (Malay Peninsula Standard Time)
Then when I use phstc's toBrowserTimeZone function, it still returns me the same datetime. I wanted to get something like 2014-06-27 15:11:16
Here is the code below:
var originalDateTime = new Date(`2014-06-27 07:11:16`);
alert(DateFormat.format.toBrowserTimeZone(originalDateTime,"yyyy/MM/dd HH:mm:ss"));
According to this statement in phstc's dateFormat page,
value = String representing date in ISO time (ā€œ2013-09-14T23:22:33Zā€) or String representing
default JAXB formatting of java.util.Date (ā€œ2013-09-14T16:22:33.527-07:00ā€) or String representing
Unix Timestamp (Sat Sep 14 2013 16:22:33 GMT-0700 (PDT)) or javascript date object.
JS Date object should work but unfortunately, it didn't. Well, I got it fixed by changing the datetime to other formats stated above first before calling the toBrowserTimeZone() function. For example,
var originalDateTime = DateFormat.format.date('2014-06-27 07:11:16',"yyyy-MM-ddTHH:mm:ssZ");
var newDateTime = DateFormat.format.toBrowserTimeZone(originalDateTime);

telerik:RadTimePicker get only time using javascript

I have telerik:RadTimePicker control and I want to get the time using javascript. In telerik web site they have mentioned
var picker = $find("<%=RadDateTimePicker1.ClientID%>");
var view = picker.get_timeView();
alert(view.getTime());
But when I tried this I get all the details including time information.
Tue Feb 26 2013 05:00:00 GMT+0530 (Sri Lanka Standard Time)
What I really need is 05:00:00
Any easy way ?
the getTime() is returning you a JavaScript Date object. You will need to use the JavaScript Date constructs to extract the Hours:Minutes:Seconds kind of data. Here is a link to JavaScript date documentation: http://www.w3schools.com/jsref/jsref_obj_date.asp
you can just use the get_selectedDate() on the picker. This method too returns you a javaScript Date object. You can then extract the hours, minutes, seconds from the date object. we don't have any specific time string returning property on the picker object model.

Categories

Resources