Solved

Display First Day of Month C#

Posted on 2010-11-16
6
666 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone 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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, 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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used.

791 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