When the page loads, I want a variable inside my JavaScript to hold today's date. So far I have this:
<script type="text/javascript">
var TodayDate = <%Eval('System.DateTime.Now') %>;
</script>
It massively bugs. Any suggestions?
Just use a javascript date object.
var today = new Date();
Or if you need the server time
var today = new Date('<%= DateTime.Now.ToString() %>');
is there anything in particular wrong with
<script type="text/javascript">
var TodayDate = new Date();
</script>
?
Why don't u use Javascript Date() object directly?
<script type="text/javascript">
var TodayDate = Date();
</script>
If you want to have a variable containing the date on the server, your way of doing is probably good (except for the missing apostrophes and the Date constructor). So it should be:
var today = new Date('<%= DateTime.Now.ToString() %>');
//this assumes that the server has such a locale that DateTime.ToString can be
//parsed by the javascript interpreter
But, if you need to have the date of the client:
var today = new Date();
You can use the previous people's answers of setting the date from within Javascript.
The problem you're seeing is that ASP is putting the string representing the current Date directly into the JavaScript source code. You need to put quotes around it so JavaScript sees a string and can parse it corretly.
Something like:
<script type="text/javascript">
var TodayDate = "<%Eval('System.DateTime.Now') %>";
</script>
which sets TodayDate to be a string. Are you going to use it as such or as a Date object?
Related
I'm trying to get the current date - 3 months and use it in a postman Pre-request script. I'm told it uses javascript, but it doesn't seem to be working.
The error I get is:
There was an error in evaluating the Pre-request Script: TypeError:
startDate.setMonth is not a function
Here is what I have:
// setup start date
var startDate = Date();
startDate.setMonth(startDate.getMonth() - 3);
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Syntax
JavaScript Date objects can only be instantiated by calling JavaScript Date as a constructor: calling it as a regular function (i.e. without the new operator) will return a string rather than a Date object; unlike other JavaScript object types, JavaScript Date objects have no literal syntax.
so
Date();
needs to be
new Date();
Try change var startDate = Date(); to var startDate = new Date();
As an alternative, Postman comes with the moment module built in so you could do something like this:
var moment = require("moment")
var startTime = moment().subtract(3, 'months')
Or you could obviously use native JavaScript, worth knowing a couple of different ways though.
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);
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());
var time =<%:Html.Raw(Json.Encode(Model.AvailableDates))%>;
var parsed = time[0];
alert( parsed );
The pop up shows: "/Date(1174021200000)/". It is not a instance of date. I tried .toString("mm/dd/yy"), Date.parse(parsed), new Date(parsed). Unfortunately, none of these works. I dont wanna let my controller return a formatted value. Is there a way I can parse it on client side? thank you. By the way AvailableDates is a list of datetime in c#.
you can parse it like this:
var date = new Date(<% Model.AvailableDates %>);
Try use moment.js
var time =<%:Html.Raw(Json.Encode(Model.AvailableDates))%>;
var parsed = moment(time);
alert( parsed );
See an example here in jsfiddle
I want to use client system date on page load event,
How can I do that.
Since this is not a post back so please let me know how to do it.
Thanks
var a = "&date=" + (new Date().replace(/\s+/g, ""));
Just append the value of a to all your links back to the server. On the serverside you will see the datetime come across in the HTTP request.
var currentDate = new Date()
The currentDate object will have the methods:
getMonth()
getDate()
getFullYear()
This should give you what you need.
Edit:
Since you mentioned time in the title of the post, it's probably worth mentioning that currentDate will also have:
getHours()
getMinutes()
should you want to display the current time as well.
Edit 2:
To tie into page loading
<script type="text/javascript">
function buildDate() {
var date = new Date();
/*build date string here*/
}
</script>
<body onload="buildDate()">
Other stuff here...
</body>