Solved

Display First Day of Month C#

Posted on 2010-11-16
6
668 Views
Last Modified: 2013-12-17
Hello,

I'm trying to avoid using Left/Right properties to extract numbers that will built a date like 11/1/2010-basically I want to display the current month, current year with day 1. Here's what I have thus far...

tmpExDate = Strings.Left(Convert.ToString(Module1.ExDateVal), 2);  //ExDateVal is a DateTime variable
    tmpExDate = tmpExDate + "-1-";
    tmpExDate = tmpExDate + Strings.Right(Convert.ToString(Module1.ExDateVal), 4);
0
Comment
Question by:jcampanali
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34150673
Much simpler option:

Dim tmpExDate As New DateTime(Module1.ExDateVal.Year, Module1.ExDateVal.Month, 1)

Open in new window

0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 25 total points
ID: 34150681
Hmm, lets try that again in C# :)

DateTime tmpExDate = new DateTime(Module1.ExDateVal.Year, Module1.ExDateVal.Month, 1);

Open in new window

0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 25 total points
ID: 34150781
this gives you the first day of month

Module1.ExDateVal.AddDays(1-Module1.ExDateVal.Day)

if you need a string, put it inside string.format

String.Format("m/d/yyyy", ... )
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 9

Expert Comment

by:shadow77
ID: 34151686

DateTime dt = DateTime.Now;
    string s = string.Format("{0:D2}-1-{1:D4}", dt.Month, dt.Year);

Open in new window

0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34151949
DateTime now = DateTime.Now;
DateTime firstDayofMonth = new DateTime(now.Year, now.Month, 1);
string s = firstDayofMonth.ToShortDateString();
0
 
LVL 7

Expert Comment

by:mkobrin
ID: 34153627
DateTime firstDayofMonth = DateTime.Today.AddDays(-(DateTime.Today.Day - 1));
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I get the up & down arrow keys to navigate in an Autocomplete textbox list. 3 50
IDE for Python 5 73
C# Service FileSystemwatcher isse 9 47
C# XML Get Values 4 37
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question