Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
3.11/5 (2 votes)
See more:
Hi friends,

i need to convert Date format from mm/dd/yyyy to dd/mm/yyyy
Here my code:

XML
<div class="span6">
  <label id="lblLiveUntil" runat="server">News Live Until</label>
  <asp:TextBox ID="txtDateTo" CssClass="span4 datepicker" runat="server"></asp:TextBox>
 </div>


so datepicker is a jquery.
it return date format as "mm/dd/yyyy"
but database accept only dd/mm/yyyy format

I need to convert this format(from mm/dd/yyyy to dd/mm/yyyy)
and insert into database in startdate field(which is also in datetime format)


Regards

Lalitha
Posted

Hi
Try this one

XML
<script>
       $(function () {
           $("#datepicker").datepicker();
           $("#format").change(function () {
               $("#datepicker").datepicker("option", "dateFormat", $(this).val());
           });
       });
   </script>


<p>
       Date:
       <input type="text" id="datepicker" size="30"></p>
   <p>
       Format options:<br>
       <select id="format">
           <option value="mm/dd/yy">Default - mm/dd/yy</option>
           <option value="yy-mm-dd">ISO 8601 - yy-mm-dd</option>
           <option value="d M, y">Short - d M, y</option>
           <option value="d MM, y">Medium - d MM, y</option>
           <option value="DD, d MM, yy">Full - DD, d MM, yy</option>
           <option value="&apos;day&apos; d &apos;of&apos; MM &apos;in the year&apos; yy">With
               text - 'day' d 'of' MM 'in the year' yy</option>
       </select>
   </p>



Please mark if it helps you
 
Share this answer
 
Try this way

XML
<script type="text/javascript">
        $(function () {
            $("#datepicker1").datepicker({ dateFormat: "mm/dd/yy" }).val()
        });

    </script>



input type="text" id="datepicker1" size="30">
 
Share this answer
 
you can achieve this by create common function and get datetime object and based on object you can get any format as you wish


DateTime expextedDate = GetFormatedDate("11/28/2013");
string expextedFormat = expextedDate.ToString("dd/MM/yyyy");

private DateTime GetFormatedDate(string currentformatdate)
{
    string[] dateparts = currentformatdate.Split('/');
    DateTime expextedDate = new DateTime (Convert.ToInt32(dateparts[2]), Convert.ToInt32(dateparts[0]), Convert.ToInt32(dateparts[1]));
    return expextedDate;
}
 
Share this answer
 
Comments
Karthik_Mahalingam 28-Nov-13 5:02am    
@lalitha, this is simple and clean, it will work fine for your requirement...
lalitha19 28-Nov-13 8:57am    
Thanks Naresh,
your solution working fine.

Karthik_Mahalingam 28-Nov-13 9:01am    
hi lalitha,

if u got the exact solution. pls click the Accept Solution button....
DateTime dt = DateTime.Parse("entered date", CultureInfo.GetCultureInfo("en-gb"));
 
Share this answer
 
It is more efficient to change the date format of datepicker during initialization, Check these out:

Initialize Datepicker

and


my example in jsfiddle[
^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900