Say the current date is 1st Mar 2010, I want to display it like this...
20100301 so like first 4 digits = year, 2 digits = Month, 2 digits = day
is there an easy way to do this?
use format
yourdatetimeObj.ToString("yyyyMMdd");
Ref: http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
Something like
dateTimeObject.ToString("yyyyMMdd");
See String Format for DateTime
var mydate = DateTime.Now; // Whatever you want.
mydate.ToString("yyyyMMdd");
Look at DateTimeFormatInfo for the other custom format strings you can use.
You can either use the ToString() implementation of the DateTime class, like the examples already given, or use a format string to display it along with other information, like so:
var now = DateTime.Now;
var msg = String.Format("Now: {0:dd/MM/yyyy}", now);
Or
Console.Write("Now: {0:MM/dd/yyyy}", now);
Related
I want to extract the name of the day as string from this:
Holder.CurrentDate = DateTime.Now.AddDays(countNext).ToLongDateString();
Holder.CurrentDay = ?
Where Holder.CurrentDay is a string. I have tried:
Holder.CurrentDay = Holder.CurrentDate.DayOfWeek.ToString();
But DayOfWeek does not exist as a function in this context. Do you guys have any ideas how I can accomplish this?
You could just do this:
var CurrentDate = DateTime.Now.AddDays( 4 ).ToLongDateString();
var CurrentDay = Convert.ToDateTime(CurrentDate).DayOfWeek.ToString();
But its better to store the DateTime itself in a variable instead of converting it back:
var theDateWeNeed = DateTime.Now.AddDays( 4 );
//Now we can do this:
Holder.CurrentDate = theDateWeNeed.ToLongDateString();
Holder.CurrentDay = theDateWeNeed.DayOfWeek.ToString();
You would be better to use ToString with a format pattern if what you ultimately want is a string. Then you have control over language, etc.
Use the "dddd" pattern to get the name of the day of the week.
var dayOfWeek = DateTime.Now.ToString("dddd");
Pass a second CultureInfo parameter if you want it in a specific language.
I found a way, sorry...
Holder.CurrentDay = DateTime.Now.AddDays(countNext).DayOfWeek.ToString();
You cannot invoke DayOfWeek on string. i.e. you have to convert it back or use the same DateTime as you did in your previous statement.
Holder.CurrentDay = DateTime.Now.AddDays(countNext).DayOfWeek.ToString();
I have the following simple example:
string dt = DateTime.Now.ToString("yyyy-MM-dd hh.mm.ss");
I can't change the DateTime.Now, but I can change datetime format yyyy-MM-dd hh.mm.ss. Following this example the result must be today's date, but I need to get yesterday's date with the same parameters except day (year, month, hours, minutes and seconds). E.g. 2015-08-23 12.09.59 must be 2015-08-22 12.09.59. So is it possible to use some "-" operator or something else inside the datetime format to achieve the result?
If you want yesterday's date, you can do this
string dt = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd hh.mm.ss");
DateTime.AddDays() lets you add number of days, positive for future date, negative for past date.
E.g. 2015-08-23 12.09.59 must be 2015-08-22 12.09.59. So is it
possible to use some "-" operator or something else inside the
datetime format to achieve the result?
No, it's not possible inside the DateTime format. you can not change any thing. Because it is only for define format of the Date to display in string format. Any addition or subtraction can only be done before converting it to string format as suggested by "Arghya C".
Can you explain your limitation so we can solve your problem.
If you can only influence the date time pattern, than use the roundtrip format and parse the returning string back to a date time, add the calculation and format it into the desired format:
var dateTimeString = badLibrary.GetDateTime("o");
var dateTime = DateTime.Parse(dateTimeString, null, DateTimeStyles.RoundtripKind);
var newDateTime = dateTime.AddDays(-1);
return newDateTime.ToString("yyyy-MM-dd hh.mm.ss");
I want to extract date from the string.
String : _21_BT_Txn_Details_1-Aug-2015_1031389ACF6.zip
How to do it?
Try this:
\d{1,2}-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4}
Demo and explanation of regex symbols: https://regex101.com/r/lW9yI3/2
Assumptions:
Day could be one or two digits
Year is always four digits
Standard three-letter abbreviations are use for the month
You can use the following code that can extract more than 1 dates inside the string like the one you provided:
var txt = "_21_BT_Txn_Details_1-Aug-2015_1031389ACF6.zip";
DateTime dt;
var res = txt.Split('_').Where(p => DateTime.TryParse(p, out dt)).ToList();
Or, if you always have the date in the above format (day-MON-year), use
DateTime.TryParseExact(p, "d-MMM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)
There is no need for a regex here.
i have a string like "14-Nov-2014" , i want to convert this string to this 14.11.2014 format.
after converting i want to add 14 days to above date.
given date is not Datetime format.
Old date="14-Nov-2014"
new date=14.11.2014
is there any way to do in c#?
Assuming,
var myString = "14-Nov-2014";
First parse the string, most likely using DateTime.ParseExact. Assuming a few things about the format you have, you could do the following. Note you most likely should specify the proper culture for the third argument:
var dateTime = DateTime.ParseExact(myString, "dd-MMM-yyyy", null);
Then you can add 14 days to it easily:
var dateTime = dateTime.AddDays(14);
To get a new string in a different format just use ToString with a format string. For example:
var myNewString = dateTime.ToString("d.MM.yyyy");
I have a date that shows up as 10/18/2011 3:12:33 PM
How do I get only the time portion of this datetime?
I am using C#.
I tried:
string timeval = PgTime.ToShortTimeString();
but that did not work as Intellisense only showed ToString();
Assuming that
DateTime PgTime;
You can:
String timeOnly = PgTime.ToString("t");
Other format options can be viewed on MSDN.
Also, if you'd like to combine it in a larger string, you can do either:
// Instruct String.Format to parse it as time format using `{0:t}`
String.Format("The time is: {0:t}", PgTime);
// pass it an already-formatted string
String.Format("The time is: {0}", PgTime.ToString("t"));
If PgTime is a TimeSpan, you have a few other options:
TimeSpan PgTime;
String formattedTime = PgTime.ToString("c"); // 00:00:00 [TimeSpan.ToString()]
String formattedTime = PgTime.ToString("g"); // 0:00:00
String formattedTime = PgTime.ToString("G"); // 0:00:00:00.0000000
If you want a formatted string, just use .ToString(format), specifying only time portions. If you want the actual time, use .TimeOfDay, which will be a TimeSpan from midnight.
DateTime PgTime = new DateTime();
var hr = PgTime.Hour;
var min = PgTime.Minute;
var sec = PgTime.Second;
//or
DateTime.Now.ToString("HH:mm:ss tt") gives it to you as a string.
Don't now nothing about a class named PgTime. Do now about DateTime, though.
Try
DateTime instance = DateTime.Now ; // current date/time
string time = instance.ToString("t") ; // short time formatted according to the rules for the current culture/locale
Might want to read up on Standard Date and Time Format Strings and Custom Date and Time Format Strings
In C# 10 you can use TimeOnly.
TimeOnly date = TimeOnly.FromDateTime(PgTime);