Solved

Display First Day of Month C#

Posted on 2010-11-16
6
659 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now