?
Solved

Display First Day of Month C#

Posted on 2010-11-16
6
Medium Priority
?
674 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 100 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 100 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
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 this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

800 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