473,804 Members | 2,143 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10802
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.co m
"Andrew Wilhite" <sw******@gmx.n et> wrote in message
news:c0******** *************** ***@posting.goo gle.com...
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.n et> 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:ddMMyyy y}",
DateTime.Now.Ad dDays(-1));

--
Jon Skeet - <sk***@pobox.co m>
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.n et> wrote in message
news:c0******** *************** ***@posting.goo gle.com...
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:ddMMyyy y}",
DateTime.Now.Ad dDays(-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
807
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 FormatString, how can you convert the String back to a DateTime? DateTime.Parse(...) doesn't use the FormatString. Now admitedly, if the format string is just "MM", it can't be done. But if the format string is "yyyyMMdd", or "ddMMMyyyy hhmmsst", it...
26
2693
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' How do i do that? Reny ---
2
1665
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 HH:MM:SS as type DateTime but I don't know how to do this! Can anyone help me please? Any comments/advice/suggestions/code-sampels much appreciate.
2
1659
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() + DateTime.Now.ToShortTimeString().ToString(); string logName = Data.Name + Data.ProductName + logDateExtention; And this throws exception during the debug that the name is not legal
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10603
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4314
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.