On my website i show people the time when they open my page. So when they opened it at 4/29/2013 10:09 AM it wil show: 4/29/2013 10:09:14 AM
This all is working fine on my localversion.
But now i have my website online and the time is showing with 5 minutes delay.
I opened it at: 4/29/2013 10:10 AM it shows me: 29-4-2013 10:05:19
Who can help me with showing the right time online.
The time i want to show needs to be the local time.
The code:
<td>Capture Time:</td> // label
<td>#DateTime.Now.ToString()</td>
The time you are showing is server time ... and you are expecting it to be the same as local time.
If your server is in a different timezone than your client machine, the time shown will be different.
You may want to consider using javascript and do this on the client instead.
Or show the server time in a standard format like UTC and the client can try to determine the difference from local time.
DateTime.Now will show you server datetime. If your server & you are located in same timezone, then I think your local system time is different from server datetime.
Also, if your server is in different timezone then you should consider converting in your timezone before showing datetime on the UI.
Convert Time in Different TimeZones in ASP.NET
This will take server side date and time. If the server is in USA and if you are in India then you will get 12 hours difference. To solve this create a hidden input field and then wire a Javascript routine to the onsubmit event for the form. This routine would populate the hidden field with the time on the client machine.
The hidden field can used with ASP.NET by using the HTML control "HtmlInputHidden" class. You just give you input control a runat="server" attribute like any other server side control.
The server can then read out this time when the form posts back. You could even wrap this up in a server control if you need to do this in a number of places.
var now = new Date();
now.format("dd/M/yy h:mm tt");
Copy this date time to a hidden field which have runat="server" property and take that value from server.
Looks like it's displaying in a different format (dd-MM-yyyy while you want MM/dd/yyyy). Make sure your culture is set correctly in the web.config on your production server:
<system.web>
<globalization requestEncoding="utf-8" responseEncoding="utf-8"
culture="en-US" />
</system.web>
Also, try specifying the format in the ToString method:
DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt");
This will display the date in the format you want. The time, however, is dependent on the server's time and won't necessarily match the time on your local machine.
Related
My Server has different time zone and I want to access my website from different timezone. So I want to display time in website according to system from which I will open website not want to set time according to server timezone.
Is there any way to solve my problem?
Please help me.
Thanks in Advance.
You can use javascript for same.
Add below code in your html (design) page
var currentdate = new Date()
This will give you current Date-time at end users machine.
var offset = currentdate .getTimezoneOffset()
This will give you current time zone offset with actual GMT time.
Using this value you can also identify the End-users time zone.
We have an web application in C# in where we have multiple pages with reportviewer controls, and almost all of them have date parameters.
That date parameters are rendering a datepicker (everything it's fine till this point, no exceptions, no problems) but all the datepickers starts on Sunday ("domingo" in spanish, "do" abbreviated) and our client complains about it; he wants it to start on monday ("lunes" in spanish, "lu" abbreviated).
Accessing the same reports from reportserver works fine, all the datepickers starts on monday("lu") and it works fine:
I've checked that user's Culture and CultureUI it's correct (es-ES for spanish culture); and i think that's working fine cause today's text and day names are properly translated...
I've Googled it and i have only found that if you change DATEFIRST in SQL it must be solved; but it doesn't.
Can anyone give me a clue or any solution?
I know that i can use my own parameter selectors and call the reportViewer only to load the report with the parameters, but this solution will take a long time and we prefer to avoid that solution.
I've also read that with Reflection i can access the Datepicker class inside Microsoft.Reporting and change it to start on monday; but it's not a "nice" solution and it's really complex.
I've had similar trouble with date-format on the client. The doco says that it should determined by the Language of the report, so set that to the language of the client (see below). You could even be more specific and set the language to es-ES
But I've found this approach does not always work. (for me at least). Once the report is set up like this, changing the client locale, should change the date format (and start day of week).
At one point changing the locale of the SSRS server had the desired effect (but this is a pretty sweeping change)
I have an application which I developed in the UK. I have now deployed this to a US server and the dates are incorrect. I have a date time picker which I have set the format to be dd/mm/yyyy, when I select the date time picker I notice that when I inspect the element the date is actually 12/10/2013.
When I store this to the database this is being transformed as 2013-12-10 and then when I retrieve this from the database it is actually 10/12/2013.
Also when I call DateTime.Now in the code this is coming back as a US format (mm/dd/yyyy). I need everything to be consistent. How?
I have tried setting the culture info in the web config <globalization uiCulture="en-GB" culture="en-GB" /> but this is having no effect, some areas i am storing the date in sql server as GETDATE() and sometimes this is passed in as DateTime
I don't actually think you're dealing with incorrect behavior, you're most likely dealing with different TEXT representations of the SAME underlying DateTime or DATE item:
If Visual Studio is running on a system with en-US regional settings, it's insepectors will obviously use those settings when displaying dates in the debugger.
If you're looking at an SQL column with a data type of DATE, SQL might use the "SQL" standard representation for string, and that's why you see "2013-12-10"
When you're using Object Inspector to inspect the DateTime value selected into a control, Visual Studio is again using the system's settings to display that date. It can't show you the actual binary representation.
The crust of the problem:
Make sure you differentiate your actual date (stored in a DateTime variable or a DATE database column) from it's TEXT representation.
Make sure you get the correct date.
Make sure your application's users see the date in the expected format.
Learn to deal with the text representations of date that only you as a developer would see (the SQL-style representation and the Visual Studio inspector representation).
I am going to migrating a system to Windows Azure. And it will used UTC time for all existing function. Is there any way to set the time zone globally? Or I need to change all the code which display the time? My application will mainly serve in a specified timezone.
I have try apply the culture and uiculture on web.config. And it does not work.
Thanks.
As per the Windows Azure Team Blog - all the timing calculations/display etc have been moved to UTC.. https://azure.microsoft.com/en-us/blog/moving-to-coordinated-universal-time-utc/
I feel you would need to change the code which display the time as well which saves the time as well..
I have a website that displays the current date using the code Datetime.Now. From what I understand, Datetime.Now is supposed to display the current time as it would appear in the viewer's current locale.
At the moment, when I test on localhost, the website is correctly determining my location (en-nz) and displaying the right date. However, when I run the site live, I'm getting a different date all together.
So how does a system determine a viewer's locale and why is there a difference between how my site is displaying Datetime.Now locally and live?
DateTime.Now has the time of the server, but not the time of the client. If you want to detect the time of the client you need to use Javascript, either by detecting the time with it or calculating it using the time zones.
In your localhost works fine, since the server and the client are in the same PC/Time Zone
Hope it helps!
No, DateTime.Now will retrieve the current time in the local time zone of the machine it's running on. In other words, the web server in your case, assuming you're writing a web app (you haven't made it clear).
If you want to display it in the local time zone of a browser, you may well be best to send down DateTime.UtcNow and write some JavaScript to convert that to the local time... or just let JavaScript work out the current time on the user's system.
As far as I know, there's no way of getting the time zone from JavaScript accurately. You can get the current offset from UTC, but that's not the same as the time zone itself. (Offsets change due to things like DST; knowing the current offset doesn't tell you when DST will kick in.)
Maybe machines have different time settings? If development and live servers are in different locations, that's quite possible. As other answers say, DateTime.Now gets server time, not client time.