Set TimeZone in AJAX CalendarExtender using C# and VB.Net in ASP.Net

Last Reply 6 months ago By dharmendr

Posted 6 months ago

hello, 

i am using ajax calender in my application, i have shared hosting and they say they cannot set Moutain date Time from server, 

how to set Ajax calender to bet set as Mountain Date Time. 

please advice

You are viewing reply posted by: dharmendr 6 months ago.
Posted 6 months ago

Hi nauna,

Refer below sample.

HTML

<script type="text/javascript">
    function clientShowing(sender, args) {
        sender.set_todaysDate(new Date('<%= ToCalenderFormat(GetDatetimewithtimezone("MOUNTAIN STANDARD TIME"),GetTimeSpanDifference("MOUNTAIN STANDARD TIME")) %>'));
            sender.set_startDate('<%= ToCalenderFormat(GetDatetimewithtimezone("MOUNTAIN STANDARD TIME"),GetTimeSpanDifference("MOUNTAIN STANDARD TIME")) %>');
            sender.set_endDate('<%= ToCalenderFormat(GetDatetimewithtimezone("MOUNTAIN STANDARD TIME").AddDays(7),GetTimeSpanDifference("MOUNTAIN STANDARD TIME")) %>');
        }
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Label ID="lblb" runat="server"></asp:Label>
<asp:TextBox ID="txtDate" runat="server" CssClass="disable_future_dates" />
<asp:ImageButton runat="server" ID="imgPopup" ImageUrl="~/Calendar.png" />
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtDate"
    PopupButtonID="imgPopup" OnClientShowing="clientShowing" />

Code

C#

public static String ToCalenderFormat(DateTime dt, TimeSpan TimeZoneDifference)
{
    return new DateTimeOffset(dt.Ticks, TimeZoneDifference).ToString("yyyy-MM-ddTHH:mm:ssZ");
}
public static DateTime GetDatetimewithtimezone(string Timezone)
{
    return (TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(Timezone)));
}

public static TimeSpan GetTimeSpanDifference(string Timezone)
{
    TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(Timezone);
    return timeZoneInfo.BaseUtcOffset;
}

VB.Net

Public Shared Function ToCalenderFormat(ByVal dt As DateTime, ByVal TimeZoneDifference As TimeSpan) As String
    Return New DateTimeOffset(dt.Ticks, TimeZoneDifference).ToString("yyyy-MM-ddTHH:mm:ssZ")
End Function

Public Shared Function GetDatetimewithtimezone(ByVal Timezone As String) As DateTime
    Return (TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(Timezone)))
End Function

Public Shared Function GetTimeSpanDifference(ByVal Timezone As String) As TimeSpan
    Dim timeZoneInfo As TimeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById(Timezone)
    Return timeZoneInfo.BaseUtcOffset
End Function

Screenshot