Transformation of time format in C #

Source: Internet
Author: User
Tags datetime iso 8601 local time string format

Sometimes we have to change the time to achieve different display effects.

The default format is: 2005-6-6 14:33:34

What if you want to change into a 200506,06-2005,2005-6-6 or more?

We need to use: DateTime.ToString method (String, IFormatProvider)

Using System;

Using System.Globalization;

String format= "D";

DateTime Date=datatime,now;

Response.Write (date. ToString (format, datetimeformatinfo.invariantinfo));

Result output

Thursday, June 16, 2005

Parameter format format verbose usage

Format Character Association attribute/description

D ShortDatePattern

D Longdatepattern

F Full Date and time (long date and short time)

F Fulldatetimepattern (long date and long time)

G General (short date and short time)

G General (short date and long time)

M, M Monthdaypattern

R, R Rfc1123pattern

s use local time Sortabledatetimepattern (based on ISO 8601)

T Shorttimepattern

T Longtimepattern

U universalsortabledatetimepattern format for displaying Universal Time

U use the full date and time of the universal Time (long date and long time)

Y, y Yearmonthpattern

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.