How to change Local datetime into UTC Format

DateTime has a Kind property, which can have one of the three values:

    Unspecified
    Local
    Utc


DateTime convertedDate = DateTime.SpecifyKind(
    DateTime.Parse(dateStr),
    DateTimeKind.Utc);

var kind = convertedDate.Kind; // will equal DateTimeKind.Utc
Now, once the system knows its in UTC time, you can just call ToLocalTime:

DateTime dt = convertedDate.ToLocalTime();
This will give you the result you require.

How to find record for Nearest current time in sql sever

Hi,

I am having three record in my table.

1      Student        03/06/2012       04:00 PM  
2    Staff     30/05/2012     02:58 PM  
3    Others     03/06/2012     10:00 AM    

In this table have two record on same date Student and Staff ( 03/06/2012 ) but time field is different. Student is  04:00 PM and Others is 10:00 AM.

How to get nearest time record using current time.

ex. my current time is 8.00 AM or 12.00 PM get record no 3. because its time 10.00 AM. It is only nearest to current time. If my current time is 03.00 PM or 06.00 PM then get record no 1. because its time 04.00 PM. It is only nearest to current time.

Solution

SELECT Top 1 * FROM tablename WHERE CONVERT(VARCHAR(10),datetimecolumnname,103) ='01/06/2012' order by ABS(DATEDIFF(minute, datetimecolumnname, GETDATE()))


CONVERT(VARCHAR(10),datetimecolumnname,103) ='01/06/2012'
------------------------------------------------------------------------------------
It will get a record on 03/06/2012

order by ABS(DATEDIFF(minute, datetimecolumnname, GETDATE()))
----------------------------------------------------------------------------------------
It will get a closest time from current time.

SELECT Top 1
------------------
it will get a first record of after performing these conditions.

 

Get Time From datetime field in SQL2005

Get Time From datetime field in SQL2005

SELECT * FROM tablename WHERE  CONVERT(VARCHAR(8),datetimecloumnName,108) >= '10:35:16 AM' AND CONVERT(VARCHAR(8),datetimecloumnName,108) <= '11:35:16 AM'


Get Date From datetime field in SQL2005 (103-as dd/mm/yyyy)

SELECT * FROM tablename WHERE CONVERT(VARCHAR(10),datetimecloumnName,103) ='02/06/2012'