Avatar of maqskywalker
maqskywalker

asked on 

C# DateTime.Now formatting

I'm using C# in a ASP.NET web application

In my C# code i have this line where I save today's date into a variable.

Todays date is 11/6/2017

With this code below. The value that gets saved in my variable looks like this: 11/6/2017 8:18:23 AM

// save todays date in a variable
DateTime testvar1 = DateTime.Now;

How do I format DateTime.Now so it save all the digits for the month and day.

I want the value saved in the variable to be this:
11/06/2017 8:18:23 AM

If today's date was 9/5/2017 the value saved in the variable would be this:
Basically add the zeros for single digit months and days.
09/05/2017 8:18:23 AM
ASP.NETC#

Avatar of undefined
Last Comment
Dmitry G
Avatar of kaufmed
kaufmed
Flag of United States of America image

Formatting a date is a display oriented task. If you're not doing something pertaining to display, then why do you care what format the date is in? A date is a date. Storage of that data should be done as the raw data (type). When you go to render the data to the screen (or a file, or a result from a web service, etc.), then stress about the format.
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

// save todays date in a variable
DateTime testvar1 = DateTime.Now;

How do I format DateTime.Now so it save all the digits for the month and day.


What you have does what you ask for.  See the comment by Kaufmed.  A DateTime contains ALL the information, none is being lost.
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Fully agree with Kaufmed and Andy above.
But whenever you ARE ready to convert (when you're displaying the variable), just use a custom format string, don't convert individually and then recombine.

DateTime.Now.ToString("MM/dd/yyyy h:mm tt")
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Honestly, I'd never let to use the above "date string manipulation" code in my group. There are standard approaches to do the job, like shown by Snarf0001.
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo