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
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
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;
}
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;
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;
'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
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);