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();
Related
I need to figure out how to get the 'when' variable in the right datetime format.
Its value is in: MM/yyyy format.
rt.Add(new RoadTrip()
{
Id = int.Parse(rec[0]),
Where = rec[1],
How = int.Parse(rec[2]),
When = DateTime.Parse(rec[3]),
WithWhat = rec[4]
});
You'll want to use DateTime.ParseExact to specify the format of the string that you want to parse to a date.
In your case it will probably use something like:
When = DateTime.ParseExact(rec[3], "MM/yyyy", CultureInfo.InvariantCulture)
It sounds like you need to use DateTime.ParseExact
When = DateTime.ParseExact(rec[3], "MM/yyyy", CultureInfo.InvariantCulture),
For a value of "10/2015" you will get a DateTime object with the value "10/1/2015 12;00:00 AM". Note that this will also throw a FormatException if the value does not match the format.
I have following code:
string date = "13.04.2012";
string date2 = (DateTime.Parse(date).AddDays(1)).ToString();
This is working correctly without a problem but after the DateTime.Parse function the variable date2 is '14.04.2012 00:00:00' but i would like to have only the date '14.04.2012' without the timestamp.
I thought about using the substring function like this:
string sub = date2.Substring(0, 10);
That would work like this but isn't there a better way to get that result?
try this
string date = "13.04.2012";
string date2 = (DateTime.Parse(date).AddDays(1)).ToShortDateString();
DateTime.Parse returns a DateTime value, which is not really a string so it's wrong to say that it has the value '14.04.2012 00:00:00'.
What you need to do here is add a format parameter to the ToString call, or use one of the convenience formatting methods.
DateTime.ToString(string format)
Try DateTime.Date property. May be it will be correct for this. See the below code part
DateTime dateOnly = date1.Date;
and out put will be
// 6/1/2008
EDIT:
or simply you can try
DateTime.ToString("dd.MM.yyyy");
I think you are after formatting
System.DateTime now = System.DateTime.Now;
System.DateTime newDate = now.AddDays(36);
System.Console.WriteLine("{0:dd.mm.yyyy}", newDate);
I have a date stamp (020920111422) and I want to split it to
day = 02,
month = 09,
year = 2011,
hour = 14,
and minute = 22
Is there a "split string at position" method in C#?
You want:
string x = s.Substring(0, i), y = s.Substring(i);
(or maybe i-1/i+1 depending on your exact requirements).
However, you could also use DateTime.ParseExact to load it into a DateTime by telling it the explicit format:
var when = DateTime.ParseExact("020920111422", "ddMMyyyyHHmm",
CultureInfo.InvariantCulture);
you can do this via SubString - for example:
string myDay = mydatestamp.SubString (0,2);
OR create a DateTime:
DateTime myDateTime = DateTime.ParseExact ( mydatestamp, "ddMMyyyyHHmm" , CultureInfo.InvariantCulture );
Answering on question considering "split string at position" - you can leverage String.Substring(Int32, Int32) method by calling multiple times with different offsets.
Also take a look at LINQ Take() and Skip() methods which allows provide count of elements to return as well.
Otherwise see examples which other guys are provided using DateTime.ParseExact(), I believe this is most correct way to convert string you've provided to DateTime value.
You could also use
var d = DateTime.Parse(s, "ddMMyyyyHHmm");
if the end-goal is a DateTime.
Instead you can convert the date stamp by using Datatime.ParseExact and can extract the day, month, year, hour and minute you want from that date stamp. Refer the following code part for Datetime.ParseExact converting.
DateTime.ParseExact(YourDate, "ddMMyyyyHHmm", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None)
For more usecase, maybe this can help
string rawData = "LOAD:DETAIL 11.00.0023.02.201614:52:00NGD ...";
var idx = 0;
Func<int, string> read = count =>
{
var result = rawData.Substring(idx, count);//.Trim();
idx += count;
return result;
};
var type = read(16);
var version = read(8);
var date = read(18);
var site = read(8);
//...
So I'm trying to figure out what I'm doing wrong with this logic. It seems straightforward and my breakpoints indicate that the evaulation in the 'if' statement is resolving as True, but sum.ppStart et al aren't getting 14 days added to them.
It's probably something simple, but any help would be appreciated.
//Determine the start/end days of each week of the pay period and retrieve a list of those entries
DateTime[] weeks = timeTools.calcPP(0);
DateTime today = DateTime.Now.Date;
if (today > weeks[3])
{
weeks[0].AddDays(14);
weeks[3].AddDays(14);
weeks[4].AddDays(14);
}
sum.ppStart = weeks[0];
sum.ppEnd = weeks[3];
sum.payDate = weeks[4];
AddDays returns a new instance of DateTime, the existing value is not changed, it is an immutable structure. When using the function, capture the result
DateTime myDate = ...
myDate = myDate.AddDays(14);
That is because you're not using the result of the AddDays method. The signature is
public DateTime AddDays(double days)
or so (see link). You need to do this:
weeks[0] = weeks[0].AddDays(14);
You need to assign the values:
if (today > weeks[3])
{
weeks[0] = weeks[0].AddDays(14);
weeks[3] = weeks[3].AddDays(14);
weeks[4] = weeks[4].AddDays(14);
}
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);