how to get the date and time in from PC2 to PC1? - javascript

Hello I'm having trouble how can I overcome this problem I search some articles but some examples provided is in PHP. As my title above how can I get the Date and Time from the PC2(Server side where I publish my project) (example of date and time is 8/12/2016 4:40 PM) -- and the current time of my PC1(client side who use the project) (example of date and time is 8/10/2016 5:00). What I want is, I want to display the server side time into one of my label in a view. And even I manipulate / change the PC1 date and time setting the label don't affect / change because it came from the PC2 time. I tried to use Datetime.now() and try to change the setting time from the PC1 and my label date also change after I refresh the page.

It seems you are working with Asp.net-Mvc, you want to display your server time to client browser. If you are using Razor template engineer, Try this please:
<div>#Html.Label(DateTime.Now.ToString())</div>
Or like this:
<label for="">#DateTime.Now.ToString()</label>

DateTime dt = DateTime.Now;
String strDate="";
strDate = dt.ToString("MM/dd/yyyy hh:mm tt"); // 08/12/2016 02:22 PM

Related

How to insert timestamp without timezone using Supabase-JS client?

I want to insert current timestamp without timezone to Supabase.
I know I can set default value of the timestamp column to NOW() but
there can be some delay in the timing and I need it precise.
I have tried Date.now() but got the error: date/time field value out of range
How can this be done?
You can insert the time from javascript by setting
new Date().toISOString();
when doing an insert or update. I want to address the first part of your statement though as the precise time is the time that data hits the server so having NOW() as the default value in Postgres is more accurate than you passing the date over the wire in my opinion.

Datetime in MST Zone

On ASP.net MVC application where I save the date on datetime field in SQL sever db.It saves like this 2015-04-22 18:43:18.967.So now I need to show it as MST (Mountain Standard Time) on client side.So how can I do that ? I can use Moment.js or any other JavaScript library for that.Thanks in advance.
If you send the timestamp to the client and you are using momentjs, then it's pretty simple
var day = moment(TS_IN_MILLISECONDS).tz('America/Denver')
With the string you provided, you can do this:
var UTCTime = moment.utc('2015-04-22 18:43:18.967').toDate();
var MSTTime = moment(UTCTime).tz('America/Denver').format('YYYY-MM-DD HH:mm:ss');
So this will depend on what local time your server is running in (or if it's just pulling UTC it'll be a bit easier). But you can yank the timezone offset like this: dateTime.getTimezoneOffset(), modify it it to reflect the offset difference between your server's timezone and MST. And then modify your original datetime to reflect the new offset.
This may be a good post for reference.
Also refer to here:
dateTime.setTime( dateTime.getTime() + dateTime.getTimezoneOffset()*[math to adjust your timezone] );

Javascript - displaying locale time from mySQL TimeDate

I am having an issue with displaying the correct time. I have a php script that when a button is clicked it inserts the CURRENT_TIMESTAMP into the database. The server is located in Arizona, I am in PST. When I call the time in my script it shows Arizona time, but I need it to show the users time. So 2015-02-18 16:06:28 Arizona time, MY time is 2015-02-18 15:06:28.
How do i get the correct time. I am using moment.js, but no matter how i format it it shows the incorrect time. I am not sure but is DST, not being considered?
var time_in = time_in;//format 2015-02-18 16:17:33
var timeIn = moment.utc(time_in, "HH:mm a").format("HH:mm a");
Moment.js parses the date as a locale date-time. So when you do moment.utc(time_in), you're converting it to UTC according to your local time (PST), shifted forward or backwards.
So what you need to do is do a moment.fn.utcOffset. Arizona is UTC-07:00, so we would want to add +7 to the offset. You can do the same using moment.fn.zone but that's getting deprecated.
var utcTime = moment.utc('2015-02-18 16:06:28').utcOffset(+7).format('YYYY-MM-DD HH:mm:ss')
// returns "2015-02-18 23:06:28" which is the UTC time
Now you have the moment in UTC, you can convert it to the client localtime:
moment(moment.utc(utcTime).toDate()).format('YYYY-MM-DD HH:mm:ss')
// returns '2015-02-18 15:06:28' (which is PST)
moment.utc(utcTime).toDate() above just converts the utc time to your local time, then formatting it with momentjs
EDIT: If possible, you should use unix timestamp when sending to server, then you don't have to deal with UTC or timezones. You can convert to local time with moment.unix(unixTimestamp).format('YYYY-MM-DD HH:mm:ss')
It looks like you are using Javascript to get the time of the client, but then not passing that to the PHP. I'm not sure how your app is structured, but you could create an input tag with the type="hidden". Then using Javascript, find the element and set it's value to Date().
Here is an example: http://jsfiddle.net/43jfefuq/
Now when you submit this form with PHP, the value in the field will be the client's local time.

Match jQuery Countdown plugin with server time

I used recently a countdown in a product page. I used Jquery Countdown which is really great. I would like to synchronize the timer with server time because if the user changes time zone then hour(s) are added to the timer and thats the main problem.
In the site of the plugin (in the tab called time zones) there are some tips but dont really get it how to combine the scripts together, not much of a backend guy.
This is a part of my code: http://jsfiddle.net/zG9Ta/4/
Please check the bottom of my js cause I included the plugin inside the JS tab (and it had to be on top).
Any help how to combine my front end code with the server time?
Thanks in advance.
Main JS :
function call(){
$('.countdownclock').countdown({until: new Date(2014, 4-1,10,09,00), timezone:+3, format: 'HMS',padZeroes: true});
}
setTimeout(call,500);
The JavaScript runs client-side, so in order to have the JavaScript Date() show the client the time in Server time, server side (assuming a controller or action class), you will need to get the server's timezone's offset as an int and pass that to the page as a parameter. Then, in your JS Date, create a UTC date, then increment that Date by the offset.
In your controller/action:
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
int tzOffset = tz.getOffset(cal.getTimeInMillis());
In your JS:
var wallTimeDate = new Date();
var utcDate = new Date(wallTimeDate.getUTCFullYear(), wallTimeDate.getUTCMonth(), wallTimeDate.getUTCDate(), wallTimeDate.getUTCHours(), wallTimeDate.getUTCMinutes(), wallTimeDate.getUTCSeconds());
var serverDate = new Date(now_utc.getTime() + tzOffset);
You can see all of the methods for JS Date at W3C JS UTC
Once you have your Date, pass that to your JQuery function and use that instead of the one you have. This should get you what you need.

Display Browser specific date/time using extjs

I have a database field called CreatedDate which is a timestamp field and holds the date and time. It currently holds the GMT time.
At the moment this field is displayed as it is on the web pages.
We want to amend this to show date/time as per browser local time zone
Can you please let us know how we can do this.
Thanks in advance
If you can represent the universal time as a count of milliseconds since the 1 Jan 1970 epoch at the server then you can have the page code construct Date instances at the client using the client time zone, but starting from that UTC reference. It's the normal behavior of the Javascript Date() constructor:
var clientDate = new Date(serverUTC);
Now exactly how you get that UTC value depends on your server language. In a JSP page it'd be pretty simple:
var clientDate = new Date(<%= whatever.getTheDate().getTime() %>);
or
var clientDate = new Date(${something.theDate.time});
Once you've got the date value as a client-side Javascript Date instance, you can just update the field(s) with the string. There are no built-in Date formatting tools, but the old standard date.js might help with its ".toString()" formatter.

Categories