I want to find january 1st of a year based on the year from datetimepicker

已答覆 I want to find january 1st of a year based on the year from datetimepicker

  • Friday, January 25, 2013 10:10 PM
     
     
    I have a issue where I select a date (m/d/yyyy) from datetimepicker.  What I need to do is, using the year from the date selected, find january 1st of that year.  Any one got any ideas?  Sounds simple enough but the selected date is a string.  It is a string because the key in the database is the date in string format.

All Replies

  • Friday, January 25, 2013 10:15 PM
     
     
    select convert(date,'01/01/'+convert(varchar(4),DATEPART(year,'12/12/2012')))

    Hope it Helps!!




    • Edited by Stan210 Friday, January 25, 2013 10:16 PM
    • Edited by Stan210 Friday, January 25, 2013 10:18 PM
    • Edited by Stan210 Friday, January 25, 2013 10:19 PM
    •  
  • Friday, January 25, 2013 10:38 PM
     
      Has Code

    Try:

    SELECT CONVERT(DATE, RIGHT('03/30/2013',4)+'0101');
    -- 2013-01-01

    You should always use YYYY-MM-DD string date format.

    Datetime/text conversion:

    http://www.sqlusa.com/bestpractices/datetimeconversion/


    Kalman Toth SQL 2008 GRAND SLAM
    Paperback: Pass SQL Exam 70-461 & Job Interview: Programming SQL Server 2012



  • Friday, January 25, 2013 10:52 PM
     
     

    I get what you are saying but I notice you have a hardcoded date in your function.  I don't have that.  I have to pull the year from the string variable containing the date selected from datetimepicker which can be any date from 1/1/2007 to 12/31/2020.  This is the code (as much as it is):

            selected_date = DateTimePicker1.Text

    where:

    selected_date is in the format mm/dd/yyyy or m/d/yyyy

    and:

    the year is the LAST four positions in the string variable

    which must be extracted and combined with '1/1' to get january 1 of that year

    Any clearer?

  • Friday, January 25, 2013 10:54 PM
     
     
    select '01/01/'+convert(varchar(4),DATEPART(year,Selected_date))

    Hope it Helps!!

  • Friday, January 25, 2013 10:57 PM
     
     
    Replace the hardCoded date value with your parameter..

    Hope it Helps!!


    • Edited by Stan210 Friday, January 25, 2013 10:57 PM
    •  
  • Saturday, January 26, 2013 12:16 AM
     
     

    Actually guys.  I found the way.  Here ya go.

            Dim sdate As Date = selected_date

            smonth = Microsoft.VisualBasic.DateAndTime.Month(sdate)
            sDay = Microsoft.VisualBasic.DateAndTime.Day(sdate)
            syear = Microsoft.VisualBasic.DateAndTime.Year(sdate)
            totals_date = "1/1/" & syear

    works!

  • Saturday, January 26, 2013 2:20 AM
     
     Answered

    Your result is not a date - it's a string. A solution that provides a date is

    Dim Totals_Date As Date = New Date(sDate.Year, 1, 1)

    Once you have a date you can express it as a string in any format you choose.

  • Saturday, January 26, 2013 3:50 AM
     
     Answered Has Code

    Acamar is correct. And the way to get a date (rather than a string) from a DateTimePicker is to use the Value property of DateTimePicker rather than the Text property:

    Dim Totals_Date As Date = New Date(DateTimePicker1.Value.Year, 1, 1)