How to show date and time in one label? - asp.net

How to show date and time in one label?
lbldatetime.Text = DateTime.Now.ToString("hh:mm:ss");
I want it in 05-10-2013 9:47am format.

Use "tt" for the AM/PM designator:
lbldatetime.Text = DateTime.Now.ToString("MM-dd-yyyy h:mmtt");
See here for the complete reference.

You can format the date and time with the ToString() Method:
lbldatetime.Text = DateTime.Now.ToString("MM-dd-yyyy h:mmtt");

You can do that by using format provider to convert date time value in particular format
for more learn about format please review this link
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Thank you.

Related

How to format date and time in hbase?

I am getting string result in my date and time. I want to know in which format it is coming.
String:
2015-08-14T22:26:41.9975398Z
Format tried:
yyyy-MM-dd'T'HH:mm:ss.sTZD
yyyy-MM-dd'T'HH:mm:ss.SSSZ
I think it was IsoString just read this docs : isostring . :)

ASP.Net VB string to date based on format used in string

I'm using a JavaScript datepicker that gives me the selected date based on the language. So when the language is Dutch I get an output like 21-09-2017 (dd-mm-yyyy) And for English 21/09/2017.
When I want to cast these Strings to Dates (CDate) I get a problem with the format. Day = Month or Month = Day. What is the best way to make a Date from a string based on the format used in the string?
A solution would be to write a function for each specific culture to handle the dates but i'm guessing there is a default function in .Net??
You can use DateTime.ParseExact to get what you want as shown here.
You can provide the format like so:
dateString = "15/06/2008 08:30" //Your Date time
format = "g" //General Fromat
provider = New CultureInfo("fr-FR") //French
result = Date.ParseExact(dateString, format, provider) //Parsed Result
this will result in: 6/15/2008 8:30:00 AM
This or course only works if you know the culture. Also you may want to check out the Date Time Format Strings found here.
Convert.ToDateTime(String).ToString("dd-MM-yyyy")
OR
DateTime.ParseExact(String, "dd/MM/yyyy", CultureInfo.InvariantCulture)

How to format the datetime in c#?

I got the current datetime by using the following code.
DateTime dt = DateTime.Now;
But it is in the format of, 2015-02-23 17:25:07.123
how to convert this to the format of, "02/23/2015"?
"But it is in the format of"
No it isn't. It's just a DateTime. If you want a particular text representation, call ToString on it, specifying the format. For example:
DateTime now = DateTime.Now;
string formatted = now.ToString("MM/dd/yyyy");
You might also want to specify the culture to use for formatting - that way you could just say "Use the right short date format" for example.
See the MSDN pages on custom date/time format strings and standard date/time format strings for more details.
Try this,
String.Format("{0:MM/dd/yyyy}", dt);

C# format of date issue

My problem: I need to get date format as "mm/dd/yyyy"
Scenario:
I have declared DateBirth as nullable DateTime.
The value I get when I use:
AdvancedObj.DateBirth .Value.ToString()
is: "13/03/2013 00:00:00"
The value I get when I use
AdvancedObj.DateBirth .Value.ToString(CultureInfo.InvariantCulture)
is :"03/13/2013 00:00:00"//This is roughly correct but, I do not need 00:00:00
I have tried this as well, but the format is correct and value is incorrect.
AdvancedObj.DateBirth.Value.ToString("dd/mm/yyyy",CultureInfo.GetCultureInfo("en-Us"))
**"13/00/2013"**
Can anybody point me, what am I missing?
Use the right format string for months - it is MM. mm is for minutes:
AdvancedObj.DateBirth.Value.ToString("MM/dd/yyyy",CultureInfo.InvariantCulture)
Also, order them correctly as above - if you want months before days, use MM/dd/yyyy, if the other way around, dd/MM/yyyy.
I suggest you take a good long read of Custom Date and Time Format Strings on MSDN.
Month are 'M'. 'm' is for minutes.
"dd/MM/yyyy"

How to convert date to mm/dd/yyyy format

I want to convert dateformat to mm/dd/yyyy. Whatever dateformat is coming in textbox, I want to convert it into mm/dd/yyyy.
First you need to get it into a datetime object. The most common standards work via:
DateTime x = DateTime.Parse(txtDate.Text);
If you expect a freaky format, you still have to know what format it is:
DateTime x;
DateTime.TryParseExact(txtDate.Text, "YYddd", out x);
Then simply output the data:
string date = x.ToString("MM/dd/yyyy");
But you really need to enforce your formatting using regex, validators, scout's honor - something.
see MSDN for full details.
You will need to parse the input to a DateTime object and then convert it to any text format you want.
If you are unsure what format you may be getting, maybe it is a good idea to restrict the user to a single format (using validation or better yet a date picker).

Resources