date format issue with asp.net and sql server - asp.net

when i'm inserting data to the sql server 2008 database through asp.net application date inserted to the database in the following format
"10 june 2011"
this is wrong and i need "dd/mm/yyyy" or "yyyy/mm/dd" format to be inserted.why does this happen

If your data type on the database side is either datetime or date (on sql server 2008), it shouldn't matter whether you insert the date as '10 june 2011' or as '6/10/2011' or as '2011-06-10'. If you see the data actually being displayed as '10 June 2011' that's more likely because your data type is varchar or nvarchar. If that's the case and the field is only meant to hold dates, I would advise you to change the data type to be actually a datetime or date.

Use DateTime.TryParseExact() method to parse the date.
string inputDate="10 June 2011";
string []format ={ "dd MMMM yyyy","dd MMM yyyy"};
DateTime date;
if (DateTime.TryParseExact(inputDate, format, CultureInfo.InvariantCulture,
DateTimeStyles.None, out date))
{
Console.WriteLine("Valid " + date);
}

Table column datatype was Nvarchar, so that it saves data in the above format which is wrong. By running the following I have solved my issue
SET DATEFORMAT DMY;
ALTER TABLE #tmpTest ALTER COLUMN MyCol DATE;

Related

Change Date format (yyyy-mm-dd) to (yyyy-mm-dd hh-mm-ss) in Oracle db at the time of retrieval

I found a strange behaviour in spring mvc where i am saving a date of birth as in DATE format also its get write in oracle db as in same format but at the time of retrieval it's getting converted to dateTimeStamp format.
Please help me.
Thanks in advance
problem solved.
DateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.S");
Date date = inputFormat.parse(inputDate);
// Format date into output format
DateFormat outputFormat = new SimpleDateFormat("dd-MM-yyyy");
String outputString = outputFormat.format(date);

SQLite ORDER BY DATE after importing .csv file

My issue is quite weird, because it won't order my query result by its date field.. This is what I had done so far:
I create a table with schema below:
CREATE TABLE temp_price_list (
kode TEXT,
nama TEXT,
merk TEXT,
jenis TEXT,
modal TEXT,
tanggal DATE,
keterangan TEXT);
Below are my csv data:
44O2-24V,SEAL-BEAM,KOITO-II,UNIVERSAL,OL3-10,2010/12/17,OCU2-201
4401-24V,SEAL-BEAM,KOITO-II,UNIVERSAL,OL3-10,2010/10/15,OCU2-201
4401-24V,SEAL-BEAM,KYOTO,BULAT-KECIL-2COP,,2010/03/31,OL311
4402-24V,SEAL-BEAM,KYOTO,BULAT-KECIL-3COP,,2013/02/06,OLU2-IO
4402-24V,SEAL-BEAM,GEN,UNIVERSAL,,2014/04/30,IEU219
"H4-7""SEGI",SEAL-BEAM,GEN JP,UNIVERSAL,,2010/04/30,IEU211
7010-BLAT,SEAL-BEAM,RRT,12V=3COP=75W,CWU2-IO,2013/02/06,IWU2=24
7010-BLAT,SEAL-BEAM,DEPO,12V=3COP=75W,SOU222=PC,2012/01/07,IWU2=24
6014-12V,SEAL-BEAM,KYOTO,BULAT BIG 7010-12V 3C0P,OAU2-201,2010/08/10,OEU2-10
6052-12V,SEAL-BEAM,KYOTO,SEGI BESAR 3-COP,CFU2-IO,2013/02/06,CSU2-10
4402-24V,SEAL-BEAM,GEN,UNIVERSAL,CEU2-22,2010/05/26,IU311
4401-24V,SEAL-BEAM,GEN,UNIVERSAL =34=CO3,CEU2-22,2010/06/30,IU311
4001-12V,SEAL-BEAM,AIKOH,UNIVERSAL,,2010/09/17,XOU229
4002-12V,SEAL-BEAM,KYOTORRT,UNIVERSAL,,2013/02/06,OLU2-IO
4002-12V,SEAL-BEAM,GEN,UNIVERSAL,,2011/04/16,CSU222
33365-87701,SYNCROMES-KEY,SB,S-75 SMALL,,2012/01/25,O3-10
33365-87503,SYNCROMES-KEY,SB,S-75 BIG,,2012/01/25,OLRT-10
33365-87503,SYNCROMES-KEY,GB,S-75 BIG,,2012/01/25,I3-11
9-33263-048-1,SYNCROMES-KEY,GTYPE,PTER 3/4,,2010/05/31,F311
8-94152-557-0,SYNCROMES-KEY,G-TYPE,PTER 1/2,,2010/03/31,F311
Then I import those data using below command:
.mode csv
.import C:/sqlite/test.csv temp_price_list
Then I'll try to fetch the data using below query:
SELECT * FROM temp_price_list ORDER BY DATE(tanggal) DESC;
However it doesn't order it by date, just as the same ordering as the input. Any idea guys ?
I'm stuck here..
Reason - The Date information in your table is not in the correct format.
How to solve
Before inserting the data in your date column, convert the data into one of the standard date formats
YYYY-MM-DD
YYYY-MM-DD HH:MM
YYYY-MM-DD HH:MM:SS
YYYY-MM-DD HH:MM:SS.SSS
YYYY-MM-DDTHH:MM
YYYY-MM-DDTHH:MM:SS
YYYY-MM-DDTHH:MM:SS.SSS
HH:MM
HH:MM:SS
HH:MM:SS.SSS
now
DDDDDDDDDD
as mentioned in the below link. http://www.sqlite.org/lang_datefunc.html
when you want to insert date to database, you can use this code.
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(new Date());
In database insert the string 'date'

How to convert string into datetime format in salesforce apex code?

I want to change datetime format so that first of all I got datetime and after that I have convert it to string with different format. Now I want to again convert this into datetime format but this gives an error, invalid date time. My code is:
DateTime startDT2 = DateTime.newInstance(selectedDate.addDays(1), initialEndTime);
system.debug('select dt2>>>'+ startDT2); **output**=2014-12-09 8:00:00
String myDate = startDT2.format('M/d/yyyy h:mm a');
system.debug('select mydate>>>'+ myDate); **output**=12/9/2014 1:00 AM
Datetime dt = Datetime.valueOf(myDate);
system.debug('date_string >>>'+ dt); **output**=invalid date/time
How to solve my problem?
From the documentation of Datetime.valueOf():
The specified string should use the standard date format “yyyy-MM-dd
HH:mm:ss” in the local time zone.
You can't use 12/9/2014 1:00 AM string and convert it to Datetime.
If the users locale is English (United States) then you can use the DateTime.parse(string) method:
Constructs a Datetime from the String datetime in the local time zone and in the format of the user locale.
Example
This example uses parse to create a Datetime from a date passed in as a string and that is formatted for the English (United States) locale. You may need to change the format of the date string if you have a different locale.
Datetime dt = DateTime.parse('10/14/2011 11:46 AM');
String myDtString = dt.format();
system.assertEquals(myDtString, '10/14/2011 11:46 AM');
Incidentally, the Salesforce StackExchange site is a great place to ask Salesforce specific questions.

Format date from the following string in asp.net

I'm new to coding in asp.net and I'm struggling to format the date I've managed to pull from our CRM content pages' live date. I've wrote the following code that manages to pull and display the date the page was created:
TheDate = DR["LiveDate"].ToString();
But it formats the date and time like this:
13/11/2012 00:00:00
How can I modify my above code to just display the date like so:
Tuesday, 13 November 2012
Or, just without the time
13/11/2012
Thanks,
I assume that DR is a DataRow, isn't it?
If DR["LiveDate"] is already a DateTime column don't convert it to a localized string via ToString. Just cast it accordingly, for example via Field extension method:
DateTime dt = DR.Field<DateTime>("LiveDate");
If it's not a DateTime but a string you have to parse it to DateTime:
DateTime dt = DateTime.Parse(DR.Field<String>("LiveDate"));
If you want Tuesday, 13 November 2012 as result use:
string result = dt.ToString("dddd, dd MMMM yyyy", CultureInfo.InvariantCulture);
If you want 13/11/2012 use:
string result = dt.ToShortDateString();
or
string result = dt.ToString("d");
if your culture uses / as separator
otherwise
string result = dt.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
Further informations: http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
for your second question, try
TheDate = DR["LiveDate"].ToString("dd/MM/yyyy")

How can I remove the Time from a DateTime value?

In my SQL database, I have a column formatted as DateTime and when I retrieve data from that column in ASP.NET, I catch it on the Date variable, than pass the value to textbox:
Dim Y As Date = dt.Rows(0)("SCH_DATE")
txtSchedDate.Text = Y.Date.ToString
but when I debug my website, the txtSchedDate.Text still gives me the full DateTime value:
7/17/2013 12:00:00 AM
is it possible to eliminate the time value here and just return the date?
Have you tried using something like
txtSchedDate.Text = Y.Date.ToString("MM/dd/yyyy")
or which ever format you wish to display.
Have a look at
DateTime.ToString Method (String)
Converts the value of the current DateTime object to its equivalent
string representation using the specified format.
Custom Date and Time Format Strings
Standard Date and Time Format Strings
Convert.ToDateTime(dt.Rows(0)("SCH_DATE")).ToString("M/d/yyy")
you can get date by txtSchedDate.Text = Y.Date.ToShortDateString()
Besides answers above, you can try converting it in SQL server
SELECT CONVERT(varchar(15), GETDATE(), 11)
Keep in mind after converting it's VARCHAR(15) instead of DATETIME.
Once you have a Date object, you can get the constituent pieces if you wish as well, like this:
Dim Y As Date = dt.Rows(0)("SCH_DATE")
txtSchedDate.Text = Y.Date.Year & "-" & Y.Date.Month & "-" & Y.Date.Day
Or you can use the custom and standard date and time format strings mentioned by others.

Resources