Thursday, January 29, 2015

Convert a datetime to specific timezone using Java Script, C#.Net, SQL

Convert a datetime to specific timezone using Java Script, C#.Net, SQL
Here for example, I am taking Korea Time Zone.
1.  In java script: - to get Korea time zone - 3600000
2.  In C#:- to get Korea time zone: - TimeZoneInfo.FindSystemTimeZoneById("Korea Standard Time");
3.  In stored procedure: - to get Korea time zone: - +09:00

Java Script:-
<link type="text/css" rel="stylesheet" href="http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<script type="text/javascript">
$(function () {
        $('#<%=txtDt.ClientID %>').datepicker({
            onSelect: function () {
                var dtPickerDate = this.value.split('/');
                $('#<%=txtDt.ClientID %>').val(getDateTime(dtPickerDate[0], dtPickerDate[1], dtPickerDate[2]));
            },
            showOn: "button",
            buttonImage: "/_layouts/images/Calendar.png",
            buttonImageOnly: true,
            buttonText: "Select date",
            dateFormat: 'mm/dd/yy',
            minDate: '0'
        });
    });
</script>
<script type="text/javascript"> 
var offset = '+9';
var month;
var day;
var var_hour;
var var_minutes;
var am_pm; 
function getDateTime(month, day, year) { 
    d = new Date();
    d.setMonth(month - 1);
    d.setDate(day);
    d.setFullYear(year);
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);
    nd = new Date(utc + (3600000 * offset)); 
    //  "The local time in " + city + " is " + nd.toLocaleString(); 
    //        var var_hour = nd.getHours();
    //        var var_minutes = nd.getMinutes();
    //        var am_pm = ''; 
    //        if (parseInt(var_hour) > 11) {
    //            var_hour = parseInt(var_hour) - 12;
    //            am_pm = 'PM';
    //        }
    //        else {
    //            am_pm = 'AM';
    //        } 
    month = nd.getMonth() + 1;
    month = month < 10 ? '0' + month : month;
    day = nd.getDate() < 10 ? '0' + nd.getDate() : nd.getDate(); 
    //var newDate = nd.getMonth() + 1 + "/" + nd.getDate() + "/" + nd.getFullYear() + " " + var_hour + ":" + var_minutes + " " + am_pm; 
    return month + "/" + '' + day + "/" + nd.getFullYear();
}
</script> 

C# Code Behind:- 
internal static DateTime getDateTime()
        {
            DateTime dateTime = DateTime.Now;
            try
            {
                TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Korea Standard Time");
                dateTime = TimeZoneInfo.ConvertTime(DateTime.Now, timeZoneInfo);
                //strToday = dateTime.ToString("MM/dd/yyyy");               
            }
            catch (Exception ex)
            {                
            }
            return dateTime;
        }

For Stored Procedure:- 
ALTER FUNCTION [dbo].[Get_Converted_Date_Time]
(
      -- Add the parameters for the function here
     )
RETURNS [nvarchar] (255)
AS
BEGIN
      -- Declare the return variable here
      DECLARE @Result [nvarchar] (255)

      -- Add the T-SQL statements to compute the return value here
      SELECT @Result = CONVERT(varchar(10), SWITCHOFFSET(convert(datetimeoffset,GETUTCDATE()),'+09:00'), 101)

      -- Return the result of the function
      RETURN @Result 
END

No comments:

Post a Comment

Featured Post

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   ...

Popular posts