Solved

Display First Day of Month C#

Posted on 2010-11-16
6
662 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

815 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

7 Experts available now in Live!

Get 1:1 Help Now