How can I get date and time separately using jQuery? - javascript

In my table date is listed as "2015-07-31 06:02:20". How can I get date and time separately using jQuery?
I used some code but it shows some errors.
var timestamp = new Date(data.responsecusthistory.created_at);
var date = timestamp.toDateString();
Error: Invalid Date

var date = data.responsecusthistory.created_at.split(" ")[0];
var time = data.responsecusthistory.created_at.split(" ")[1];

If you want to have a time string (i.e. HH:MM:SS), try e.g. this:
var timestampTime = timestamp.toTimeString().split(' ')[0];
Anyway, there's no obvious reason why you get the "Error: Invalid Date", as long as
data.responsecusthistory.created_at
is a correct value, such as "2015-07-31 06:02:20". Please consider double-checking this variable.

Try this:
dateString= "2015-07-31 06:02:20";
temp = new Date(dateString);
dateStr= $.datepicker.formatDate('mm/dd/yy', temp );
for getting different formats check the link https://github.com/phstc/jquery-dateFormat.
Using different formats we will get different date, time etc in which ever forms we need it.

Related

How can I convert the value I get with datetimepicker to date and time?

var innerDate = document.getElementById('datetimepicker3').value;
The format I get from this line is: innerDate: "2021/07/07 14:00"
But I need to parse the date and time information here. How can I convert Start Date and Start Time into two separate variables?
I would recommend to parse that string with moments.js and then depending on your needs either get the individual parts for calculations or apply needed formatting
Example available at https://jsfiddle.net/Ly6qen3w/
let innerDateInput = "2021/07/07 14:00";
let innerDateMoment = moment(innerDateInput, "YYYY/MM/DD hh:mm");
console.log("Date:"+innerDateMoment.format("YYYY-MM-DD"));
console.log("Time:"+innerDateMoment.format("hh:mm"));
Try this:
var innerDate = new Date(document.getElementById('datetimepicker3').value)
Or you can attach an event handler to get the date whenever it changes
<script type="text/javascript">
$("#datetimepicker3").on("dp.change", function (e) {
let date = e.date;
console.log(date);
// no need to convert to a date
});
</script>
You can simple get that in different variable using various methods that are provided by JavaScript to get everything.
You can do is(id it's a date object else you have to convert it to date object)
let startDate = new Date(innerDate).getDate() + "/" + new Date(innerDate).getMonth() + "/" + new Date(innerDate).getFullYear();
let startTime = new Date("2021/07/07 14:00").getHours() +":"+ new Date("2021/07/07 14:00").getMinutes()
If you want something else you can retrieve that too with methods such as milliseconds,UFC time etc.
If you find this messy I would suggest you to use moment.js.
https://momentjs.com/

getEventsForDay() formatting issues with datepicker return value

I'm trying to use the Calendar API to pull up some events based on datepicker output. The issue that i'm facing with my AppScript is formatting properly the value that i get from the datepicker that will serve as input for the getEventsForDay() function.
function testing(){
var z = CalendarApp.getCalendarsByName("X")[0];
var date = new Date('2016-10-12');
var dateformatted =Utilities.formatDate(date), "GMT", "yyyy-MM-dd hh:mm:ss a");
var a = z.getEventsForDay(dateformatted, {search: 'OOO'});
The output of a into this scenario is a empty object - which is expected because this formatting is not working at all. (i've read 1000 posts that this should work).
For context as well, i have one working example with today's date, which it works fine becase the input is a new Date(). Here you go:
var datetoday = new Date();
var b = z.getEventsForDay(datetoday, {search: 'OOO'});
Any ideas on what i'm missing here?
Thanks in advance.
Since new Date() returns the Date object in UTC and not the local timezone, you may be querying the wrong day and hence the empty object if the other day does not have any events.
You can covert the date to the current timezone like
date.setTime(date.getTime() + date.getTimezoneOffset()*60*1000)
This should return the date in the local timezone and you will get the events for the correct day.
Hope it helps
Made some changes and now this works on the App Script:
var t = "2016-10-01";
var q = t.replace(/-/g,"/");
var a = new Date(q);
a.setTime(a.getTime() + a.getTimezoneOffset()*60*1000)
var w = z.getEventsForDay(a, {search: 'OOO'});
Logger.log(a);
Logger.log(w);

Date function returns NaN if variable given as input

I am using google chrome and trying to build a messaging system. I need to display date/time of messages. I used date function but it is giving 'NaN' error. It works fine if I directly pass value, but with a variable input. It fails. I searched for the answer but I am unable to identify problem. below is my code.
var d = jsarr[j];// sets d = 1448702771436
alert(d); //1448702771436
var date = new Date(d);
alert(date); //invalid date
I tried parsing 'd', with parseInt() and now it works fine. #Andreas I even tried parsing well before but somehow it was not working back then. Thanks any ways.
Might be because of uncleared cache, I hard refreshed. using ctrl + F5
var d = jsarr[i];
var s = parseInt(d);// changing type to Int
var date = new Date(s); // valid date

TypeError: Date is not a constructor

So, I've been making forms for my company for some time now with pretty easy Javascript that has worked for me in the past. However all of a sudden it's kicking out the error: TypeError: Date is not a constructor
The Code:
var Date = this.getField("Text1");
Date.value = util.printd("mm/dd/yyyy",new Date());
It works on all my old forms, but now it won't work on new ones... and I've tried making a new button on an old form - just copying and pasting the code, and then it'll break all the other buttons and spit out the same error.
Running: Windows 7 64-bit with Acrobat XI 11.0.10
The variable Date is hiding the global function Date and causing this error. Because of how scoping works in JS, the inner-most use of a name is the one that matters.
In this case, you declare var Date which becomes the only Date the function knows about. When you assign it a field or text (Date = this.getField...), you hide the global class.
You can rename your variable (I would suggest date, as capital names are typically reserved for types) or explicitly reference new window.Date when you go to construct a new date.
This worked for me:
var d = new window.Date();
Might be this answer will be helpful in future. I was using below code
var dateTime=new date();
But right code is
var dateTime=new Date();
You can't define a variable called "Date" because there's a built-in object in JS called that (you're using it in your code, actually). Change the name to something else.
var Date= somthing; <-- wrong declare, you should not use build -in object name
I was having this problem and I solved it! don't use "Date" as variable because this causes conflict with Global function Date();
Exemple: Wrong !
var Date = new Date();
document.getElementById('dateCopy').innerHTML = Date.getFullYear();
Right:
var DateTime = new Date();
document.getElementById('dateCopy').innerHTML = DateTime.getFullYear();
In your case:
var DateTime = this.getField("Text1");
DateTime.value = util.printd("mm/dd/yyyy",new Date());

Full Calendar Get current date

I've been learning Ruby over the last year and I'm very new to JS so I'll try to explain this as best I can.
I am using Adam Shaw's full calendar plugin. All I want to do is get the current month I am viewing (and use that to limit how far in the future or past a user can navigate, but that's not the problem).
I can get the current date, sort of. But, because of my lack of JS knowledge I'm not sure how to access the date.
Here is the relevant section of the config file,
viewRender: function(view){
var maxDate = "<%= finish.strftime('%Y/%m/%d') %>";
var currentDate = $('#calendar').fullCalendar('getDate');
console.log(currentDate);
if (view.start > maxDate){
header.disableButton('prev');
}
}
When I inspect the console log I see this being output as I click through the months.
So as you can see it is displaying the current date in view. My question is how do I access the _d bit of the Moment variable so I can use it?
My understanding would be that the Moment is class instance and the stuff in the dropdown is like its attributes, would this be a correct interpretation?
To get the current date of the calendar, do:
var tglCurrent = $('#YourCalendar').fullCalendar('getDate');
This will return the date as a moment object. You can then format it as a string in the usual date format like so:
var tgl=moment(tglCurrent).format('YYYY-MM-DD');
For the actual time, use the format: YYYY-MM-DD LTS
FullCalendar's getDate returns a moment object, so you need moment's toDate() method to get date out of it.
So, in you code try:
console.log(currentDate.toDate());
and that should return a date object.
var moment = $('#YourCalendar').fullCalendar('getDate');
var calDate = moment.format('DD.MM.YYYY HH:mm'); //Here you can format your Date

Categories

Resources