473,378 Members | 1,152 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

DateTime format/manipulation question

Hello,

I am just learning C# and I have ran into a problem that I cannot seem
to resolve. I need to capture today's date, subtract a day, and then
put it into the MM-DD-YYYY format. I know how to do one or the
other, but i cannot figure out how to do both. Here is my code
snippet:

DateTime Date1 = System.DateTime.Now.AddDays(-1);
string Day = System.Convert.ToString(Date1.Day);
string Month = System.Convert.ToString(Date1.Month);
string Year = System.Convert.ToString(Date1.Year);
string Logs = "log" +Year+Month+Day+"*.xml";

I need the string logs to be yesterdays date and have leading zero on
the month.

Any help would be appreciated!
Nov 16 '05 #1
4 10783
Andrew,

You can use a custom format string to generate the date. You want to do
this:

// Get the datetime (btw, your code to do this is right).
DateTime Date1 = System.DateTime.Now.AddDays(-1);

// Store the log string.
string Logs = string.Format("log{0:yyyyMMdd}*.xml", Date1);

Notice that you can place the formatting string along with the
placeholder.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Andrew Wilhite" <sw******@gmx.net> wrote in message
news:c0**************************@posting.google.c om...
Hello,

I am just learning C# and I have ran into a problem that I cannot seem
to resolve. I need to capture today's date, subtract a day, and then
put it into the MM-DD-YYYY format. I know how to do one or the
other, but i cannot figure out how to do both. Here is my code
snippet:

DateTime Date1 = System.DateTime.Now.AddDays(-1);
string Day = System.Convert.ToString(Date1.Day);
string Month = System.Convert.ToString(Date1.Month);
string Year = System.Convert.ToString(Date1.Year);
string Logs = "log" +Year+Month+Day+"*.xml";

I need the string logs to be yesterdays date and have leading zero on
the month.

Any help would be appreciated!

Nov 16 '05 #2
Andrew Wilhite <sw******@gmx.net> wrote:
I am just learning C# and I have ran into a problem that I cannot seem
to resolve. I need to capture today's date, subtract a day, and then
put it into the MM-DD-YYYY format. I know how to do one or the
other, but i cannot figure out how to do both. Here is my code
snippet:

DateTime Date1 = System.DateTime.Now.AddDays(-1);
string Day = System.Convert.ToString(Date1.Day);
string Month = System.Convert.ToString(Date1.Month);
string Year = System.Convert.ToString(Date1.Year);
string Logs = "log" +Year+Month+Day+"*.xml";

I need the string logs to be yesterdays date and have leading zero on
the month.


string logs = string.Format ("log{0:ddMMyyyy}",
DateTime.Now.AddDays(-1));

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Hi,

string s = DateTime.Today.AddDays( -1).ToString( "MM-dd-yyyy");

Cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Andrew Wilhite" <sw******@gmx.net> wrote in message
news:c0**************************@posting.google.c om...
Hello,

I am just learning C# and I have ran into a problem that I cannot seem
to resolve. I need to capture today's date, subtract a day, and then
put it into the MM-DD-YYYY format. I know how to do one or the
other, but i cannot figure out how to do both. Here is my code
snippet:

DateTime Date1 = System.DateTime.Now.AddDays(-1);
string Day = System.Convert.ToString(Date1.Day);
string Month = System.Convert.ToString(Date1.Month);
string Year = System.Convert.ToString(Date1.Year);
string Logs = "log" +Year+Month+Day+"*.xml";

I need the string logs to be yesterdays date and have leading zero on
the month.

Any help would be appreciated!

Nov 16 '05 #4
> string logs = string.Format ("log{0:ddMMyyyy}",
DateTime.Now.AddDays(-1));


Thank you! That worked perfectly and the code is a lot cleaner than before.
Thanks again!

Andrew
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: nobody | last post by:
I know that given a FormatString and a DateTime you can use DateTime.ToString(...) to convert the DateTime to a String. My question is how can you turn that around? Given a String and a...
26
by: Reny J Joseph Thuthikattu | last post by:
Hi, I have a variabe in the format of 'DD-MON-YYYY HH:MI AM' .I want to add a miniute to it.How can i do that? by manipulation i want to make '01-JUNE-2004 11:59 PM' to '02-JUNE-2004 12:00 AM'...
2
by: almurph | last post by:
Hi, Hope that you can help me with this. I'm calling DateTime.Now and storing in a DateTime container. I see that the pattern format is: m/ dd/yyyy I need it to be in the format: yyyy-MM-dd...
2
by: csharpula csharp | last post by:
Hello, I am trying to add Date and Current time to a name of a log file. I tried doing this: string logDateExtention = DateTime.Now.ToShortDateString().ToString() +...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.