Convert Jquery date to custom timezone by timezone id - javascript

I have to display datetime value on my real time page and i have done following jquery function for that.
function DisplayTimer() {
var x = new Date();
$('#<%=lblTimer.ClientID %>').html(x.toString());
setTimeout('DisplayTimer()', 5000);
}
now i have timezonid value in my session object how can i convert above date value to custom timezone using timezonid session value and also want set datetime format as per culture of user's browser through this jquery function. I have solution in server side code so using [webmethod] i can do that it will make separate request for that every 5 second so i would like to do that without server side interaction. please help me if anyone done this type of logic.
Thanks in adavance.

Change your code with:
var ClientDatetime = x.getMonth() + "/" + x.getDate() + "/" + x.getYear() + " "
+ x.getHours() + ":" + x.getMinutes() + ":" + x.getSeconds();
take one hidden variable hdnClientDateTime with runate = server and set value as following
hdnClientDateTime.value = ClientDatetime;
Now, Pass hdnClientDateTime.value variable in your server side Datetime format function and assign the value into label like this:
$('#<%=lblTimer.ClientID %>').html(Result);

Related

Getting ISO-8601 formatted date using Javascript in a Cosmos DB stored procedure

I'm writing a stored procedure for Cosmos DB. I am not very familiar with JavaScript.
I've been trying to get an ISO-8601 formatted Date, but have been unsuccessful so far. According to some docs I found, I should just be able to call toISOString on a Date. That does not work ("Object doesn't support property or method" error).
So I found this advice to define the prototype yourself:
function storedProcedure(arg) {
if (!Date.prototype.toISOString) {
// Here we rely on JSON serialization for dates because it matches
// the ISO standard. However, we check if JSON serializer is present
// on a page and define our own .toJSON method only if necessary
if (!Date.prototype.toJSON) {
var toISOString = function (date) {
function f(n) {
// Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
}
return date.getUTCFullYear() + '-' +
f(date.getUTCMonth() + 1) + '-' +
f(date.getUTCDate()) + 'T' +
f(date.getUTCHours()) + ':' +
f(date.getUTCMinutes()) + ':' +
f(date.getUTCSeconds()) + 'Z';
};
}
Date.prototype.toISOString = Date.prototype.toJSON;
}
var now = Date.now();
var iso8601 = now.toISOString();
console.log("ISO " + iso8601);
// other code
}
However, this still fails with:
Object doesn't support property or method 'toISOString'
I tried removing the prototype altogether and just using a function that takes a Date instead. However, then the same error occurred but for other members, like getUTCFullYear.
I tried also calling getSeconds and that failed for the same reason. Out of desperation, I tried listing out the object's properties using the getKeys function in this answer and it gives me an empty list.
What is going on here? How can I just get an ISO-8601 formatted Date representing the current time in UTC?
OK, it seems it was because I had used Date.now() per these docs instead of new Date() per this random google result.
I have no idea why this matters or how I could have known this (explanations welcome) but that was the underlying cause. I don't even need the prototype now.

Angularjs, date input : enter a date manually with a specific format

I need to user a date component in Angularjs.
The users asked me allow them to enter the date manually (not with a datepicker) and to put a mask on the input : (__/ __ / __).
Also, important, the date has to have this format : dd/mm/yy.
I have searched on internet but couldn't find such a component.
Does anyone know if it exists?
Thanks in advance.
This blog entry could help
ngModel.$parsers.push(function(data) {
// convert data from view format to model format
var splitted = data.split('.');
var convertedDate = splitted[1] + "/" + splitted[0] + "/" + splitted[2];
return new Date(Date.parse(convertedDate)); // converted
});

Date using only Javascript

Edit: Previously only HTML was there but in new update even Javascript works but purely Javascript ... I can't add HTML there...
I am working on a WordPress site, I need to display an image dynamically in a site. Image source will be like:
site.com/files/year/month/date/img1.jpg
But in my theme I can only input either HTML or JS there... generally if it is a static site then we can only use that image but my image gets updated daily... i.e.
today it will be site.com/files/2015/4/13/img1.jpg
tomorrow it will be site.com/files/2015/4/14/img1.jpg
How can I achieve this?
var image = document.getElementById('yourImagesId');
var date = new Date();
var url = 'site.com/files/' + date.getFullYear() + '/' + (date.getMonth() +1) + '/' + date.getDate() + '/img1.jpg';
image.setAttribute('src',url);
HTML is Hyper-Text Markup Language. It isn't "code" in the sense that it doesn't have any logic or power. It exists to literally markup your copy; that is, it's primary use is to define the layout of the content of your page.
To do anything dynamic, you need an actual coding language that can perform actual logic functions, so you'll have to utilize JavaScript (or PHP, as you're in Wordpress).
I'd be lying if I said I had experience in Wordpress's environment, but I'd imagine that googling "wordpress dynamic image path plugin" would possibly yield some tangible results.
Not sure what do you mean only with HTML, as for Javascript:
var currDate = new Date();
var url = "site.com/files/" + currDate.getFullYear() + "/"
+ (currDate.getMonth() + 1) + "/" + currDate.getDate()
+ "/" + imageName + "." + imageFormat;
getMonth will get the month as a number from 0 to 11 so you add 1 if needed to have it as 1 ~ 12.
JSFiddle Example
Be aware that JS takes the date and time from the client.

Convert Javascript Date to ASP.NET Date format

I would like to convert a Javascript date format to ASP.NET date format.
2012-09-10 12:00PM to /Date(1347442050050-0700)/
Because I'm passing it back to the server. I got the ASP.NET format from the request I did on the server, then convert it to Javascript date using moment.js:
moment("/Date(1347442050050-0700)/").format("YYYY-MM-DD hh:mmA");
Is there a way to do this?
I got what i need. If this is somehow wrong please comment.
var test = moment("2012-09-10 12:00PM").valueOf();
var test2 = moment("2012-09-10 12:00PM").format("ZZ");
var test1 = "/Date("+test+test2+")/";
alert( test1 ); // returns /Date(1347206400000+0800)/
var string = moment(test1).format("YYYY-MM-DD hh:mmA");
alert( string );​ // returns 2012-09-10 12:00PM
You can add the function to the moment prototype so that it's a little more portable.
http://jsfiddle.net/timrwood/qe8pk/
moment.fn.toASP = function () {
return '/Date(' + (+this) + this.format('ZZ') + ')';
}
If you want to send a date back to an ASP.NET ASMX web service where the RPC method receives a DateTime object, this may be helpful: https://stackoverflow.com/a/12973157/1145963

How long does my AJAX call take?

I want to do something like
var date = new Date();
var pretime = date.getTime();
$.post(
"ajaxfile.php",
object,
function(data) {
var totalTime = date.getTime()-pretime;
$("#feed").append("Time: " + totalTime + "<br/>" + pretime + "<br/>" + date.getTime() + "<br/>");
});
});
That is, measure how long the AJAXcall lasts before I get a response. But the print from this callback function is:
Time: 0
1326184886814
1326184886814
What is the solution to this?
getTime() is returning the same value because you are reusing the same Date() object. You need to create a new Date object:
var date = new Date();
var pretime = date.getTime();
$.post("ajaxfile.php", object, function(data){
var date2 = new Date();
var totalTime = date2.getTime()-pretime;
$("#feed").append("Time: " + totalTime + "<br/>" + pretime + "<br/>" + date.getTime() + "<br/>");
});
});
I'm no Javascript expert, but it seems to me that you're creating a single Date object which (if it's similar to Java's Date object) stores the date/time at the point it was created, and then using that same date object twice - i.e. comparing the start date/time to itself.
Try creating a second Date object inside the AJAX callback function to capture the end time.
If you are just interested in the time, without saving it somewhere, you can use Google Chrome's developer console.
press F12, go to "Network" tab, execute your Ajax call, and you can see a timeline of how long it takes you to get a response.
If you use firebug to debug your Javascript code take a look at the console. It will tell you how many miliseconds your ajax call takes :)
Also, if you don't use firebug, what are you waiting for? It is awesome for debugging and will save you lots of time!

Categories