Reputation: 705
I have a hidden field in my master page. And I set the current date and time as a value in that "hiddenfield" using Javascript. I am unable to get this hidden field value in any other page's page load method.
Here is my code.
HiddenField hdnCurrentDate = (HiddenField)this.Master.FindControl("hdnCurrentDate");
ClientScript.RegisterClientScriptBlock(this.GetType(), "Message", "var CurrentDate = new Date(); $('#" + hdnCurrentDate.ClientID + "').val(CurrentDate);alert($('#ctl00_hdnCurrentDate').val());", true);
I have defined Hiddenfield in master page like
<asp:HiddenField ID="hdnCurrentDate" runat="server" />
I got the alert of "hdnCurrentDate" is undefined. This is because I wrote the code in page load in not post back method.
Here is the other way what I have implemented.
I have used ConvertTimeBySystemTimeZoneId in code behind. Here is the code for the same.
DateTime ClientDateTime = DateTime.Now;
DateTime _localTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(ClientDateTime, "Arab Standard Time");
return _localTime;
I didn't get destination time zone ID instead of "Arab standard Time". If I will get it, my problem will be solved.
Is there any other way to get the current date time in any of my page. I don't want to use Session and cookies.
Thanks in advance.
Upvotes: 2
Views: 41695
Reputation: 11
If you are using ASP.Net MVC use the following code
create a class "GetTime". and in this class you will find "Egypt Standard Time" you can pick your standard time zone from this list here
public class GetTime{
public static DateTime GetCurrentTime()
{
DateTime serverTime = DateTime.Now;
DateTime _localTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(serverTime, TimeZoneInfo.Local.Id, "Egypt Standard Time");
return _localTime;
}
}
and in Global.asax add the line in the "Application_Start()"
GetTime.GetCurrentTime();
Then when you need to get the time you may write your code like this
var localTime = GetTime.GetCurrentTime();
var mytimeVarialbe = localTime.Hour;
and don't forget to import the class
using yourproject.Folderthatcontainclass
Upvotes: 1
Reputation: 110
How about use CulturalInfo From C# to get it?
string currentFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
this will return "dd/MM/yy" or "dd/M/YYY" any format in client
Upvotes: 0
Reputation: 204
Below is the javascript function to get client current datetime:
<script type="text/javascript">
function GetDate(date) {
CurTime = new Date(date);
var offset = (new Date().getTimezoneOffset() / 60) * (-1);
var utc = CurTime.getTime() + (offset * 60000 * (-1));
var serverDate = new Date(utc + (3600000 * offset));
var dateString = (serverDate.getMonth() + 1) + "/" + serverDate.getDate() + "/" +
serverDate.getFullYear() + " " + serverDate.toLocaleTimeString("en-US", { hour12: true });
}
</script>
You can also get OffSet Time from code behind as below:
public static TimeSpan GetOffSetTime(string date)
{
DateTime d = Convert.ToDateTime(date);
TimeZone zone = TimeZone.CurrentTimeZone;
TimeSpan local = zone.GetUtcOffset(d);
return local;
}
Upvotes: 3
Reputation: 9460
modified code from This link:
<script type="text/javascript">
function getDateTime()
{
var localTime = new Date();
var year= localTime.getYear();
var month= localTime.getMonth() +1;
var date = localTime.getDate();
var hours = localTime .getHours();
var minutes = localTime .getMinutes();
var seconds = localTime .getSeconds();
//at this point you can do with your results whatever you please
}
</script>
At this point i would concatenate all of the fields together and put them in an asp:HiddenField control so they can be read on the server. On the server, call Convert.ToDateTime() on the Text value of your HiddenField.
if you don't use a masterpage you should try this.
HiddenField hdnFieldValue = (HiddenField)PreviousPage.FindControl("hdnField");
Updated:
Put a hidden field on the page.
<asp:HiddenField ID="hidChild" runat="server"/>
in your javascript function
var childHidden = document.getElementById('<%hidChild.ClientID%>');
childHidden.value = window.opener.document.getElementById('text1').value;
Now access this hidden control on the page load event.
Response.Write(hidChild.Value); or Session["ParentVal"]=hidChild.Value;
Upvotes: 0
Reputation: 9862
I have done this by using following way:
I have taken a HiddenField
into the previous page where it requires :
<asp:HiddenField ID="hdnTimeOffset" ClientIDMode="Static" runat="server"
Value="" />
Then, I am getting browser timezone by this :
$(document).ready(function () {
var offset = new Date().getTimezoneOffset();
$('#hdnTimeOffset').val(offset);
});
I am getting time zone into a Session
variable of the previous page, before redirecting to the next page, like on button click:
protected void btn_Click(object sender, EventArgs e)
{
Session["TimeOffset"] = hdnTimeOffset.Value;
Response.Redirect("~/Root/abc.aspx");
}
In the Main Page, I get the current time by using following code :
DateTime dtmCurrentDateTime = DateTime.Now.ToUniversalTime();
double dblMinutes =Convert.ToDouble(Session["TimeOffset"].ToString());
dtmCurrentDateTime = dtmCurrentDateTime.AddMinutes(-dblMinutes);
Upvotes: 2
Reputation: 1210
Master Page
set servertime
<asp:hiddenfield runat="server" id="HiddenField1"></asp:hiddenfield>
protected void Page_Load(object sender, EventArgs e)
{
HiddenField1.Value = DateTime.Now.ToString();
}
set client machine time
<script type="text/javascript">
function setvalue() {
document.getElementById('HiddenField1').value = new Date();
}
</script>
<body onload="setvalue();">
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="HiddenField1" runat="server"/>
</div>
</form>
</body>
Content Page
HiddenField hdnvalue = this.Master.Master.FindControl("HiddenField1") as HiddenField;
string currenttime=hdnvalue.Value;
Upvotes: 0
Reputation: 3198
'Is there any other way to get the current date time in any of my page. I don't want to use Session and cookies.' In that case you can use JavaScript
Also, you don't need to use RegisterStartupScript
. You can just call the GetDate()
function in the onLoad
event.
<script type="text/javascript">
window.onload = function(){
getDate();
};
function GetDate()
{
var dt = new Date();
var element = document.getElementById("MainContent_FormView1_Label1");
element.text = dt.toDateString();
}
</script>
However, you might want to read this SO thread for best practice in using window.onload
, or consider using a framework such as jQuery and its document.ready
Upvotes: 0