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")
Related
I've got 1 model.DateTime value (which is I'm getting from Database) and I want to represent model DateTime which is end time, and DateTime.Now as now time.
I want to format subtract of that 2 DateTime-s as a specific String format "dd:hh:mm:ss".
In your ASP .Net view page you can open razor block and you can calculate difference between 2 variables that are DateTime type.
#{
DateTime end = (DateTime)item.close_date_time;
DateTime now = DateTime.Now;
TimeSpan diff = end.Subtract(now);
}
Then you can simply convert TimeSpan diff to string:
#diff.ToString(#"dd\:hh\:mm\:ss")
And by that I've solved time difference and to format it to specific string.
EDITED
Simply you can do this:
DateTime CurrentDateTime = DateTime.Now;
item.duration.Subtract(CurrentDateTime).ToString(#"dd\:hh\:mm\:ss");
Hope this will help
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.
I am trying to retrieve DateTime data type data from database in Asp.Net. Here is the code:
DateTime date = DateTime.Parse(dr["packingDate"].ToString());
However, the problem is the date I retrieved display in such format : 26/11/2013 12.00 AM . I just wanted to display the date but not the time. I'd tried to do this:
DateTime date = DateTime.Parse(dr["packingDate"].ToString());
string dateStr = date.ToString("dd/M/yyyy", CultureInfo.InvariantCulture);
However, it does not works in my grid view. I wonder why is it so?
Thanks in advance.
Use the built-in to string function for jsut the Date value...
DateTime.ToShortDateString();
Select the corect type from database, so in this case datetime. Then you get a DateTime in your DataTable which you can cast with the DataRow's Field extension method:
DateTime date = dr.Field<DateTime>("packingDate");
If you only want to diplay the date:
string dateOnly = dr.Field<DateTime>("packingDate").ToShortDateString();
or
string dateOnly = dr.Field<DateTime>("packingDate").ToString("d");
or
string dateOnly = dr.Field<DateTime>("packingDate").ToString("dd/MM/yyyy", CultureInfo.InvariantCulture);
You could format on the GridView control, for sample:
<asp:BoundField DataField="packingDate" HeaderText="PackingDate"
DataFormatString="{0:dd-M-yyyy}" />
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.
Im using SQLite and trying to get my date into a blackberry datetime field.
in the DB its stored as string in format:
yyyy-MM-dd HH:mm
e.g. : 2012-02-01 15:45
But the field wants it as a Long.
date - The initial date value for this field. This is the number of milliseconds since midnight on January 1, 1970. To create an empty date field, set this parameter to Long.MIN_VALUE. This method will then remove the date value from this field, setting it to null.
And then convert it back again.
Check following code:
// conversion - string to long
long dateLong = HttpDateParser.parse("2012-04-17 16:09");
// conversion - long to string
Date dateObject = new Date(dateLong);
String dateStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm")).format(dateObject);