Using NodaTime to parse an input and output different dateTime formats

theGreenCabbage

I am currently using NodaTime to parse dates and output dates

public static string nodaTimeTest6(string input)
{
    var defaultValue = new OffsetDateTime(new LocalDateTime(2000, 1, 1, 0, 0), Offset.Zero);
    var pattern = OffsetDateTimePattern.Create("yyyy-MM-dd'T'HH:mm:sso<m>", CultureInfo.InvariantCulture, defaultValue);
    var result = pattern.Parse(input);

    return result.Value.Month + "/" + result.Value.Day + "/" + result.Value.Year + " " + result.Value.ClockHourOfHalfDay;
}

The input, for example, is this: 2014-03-11T02:00:00-07:00

If my return statement is the following: return result.Value.ToString(), then the output would look like this: 2014-03-11T02:00:00-07

I understand the use of properties with NodaTime (which is a life saver), however, I am interested in outputs like this:

yyyy-MM-dd HH:mm:ss

yyyyMMdd HH:mm:ss

dd/MM/yyyy hh:mm

So I tried to change my return statement to this:

return result.Value.Month + "/" + result.Value.Day + "/" + result.Value.Year + " " + result.Value.Hour + ":" + result.Value.Minute;

The final output of that format is: 3/11/2014 2:0

Is there anyway to craft the output so it's a fixed format like 03/11/2014 02:00

I know that if I input an 01 as my month, the output would be 1/11/2014 instead of 01/11/2014

ANeves thinks SE is evil

You can send the format to the ToString method:

return result.Value.ToString("dd/MM/yyyy HH:mm", CultureInfo.InvariantCulture);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using NodaTime to parse an input and output different dateTime formats

From Dev

Serialize and then Deserialize DateTime using different formats

From Dev

Parse datetime in multiple formats

From Dev

Build a simple parser that is able to parse different date formats using PyParse

From Dev

Build a simple parser that is able to parse different date formats using PyParse

From Dev

Swift parse string with different formats

From Dev

Compare Two different formats of the DateTime

From Dev

How to use different NumberStyles as input(parse) and output(ToString) styles?

From Dev

pandas to_datetime formats date from same column in different formats

From Dev

Two different string formats for parsing DateTime

From Dev

Java or Scala fast way to parse dates with many different formats using java.time

From Dev

Content Negotiation when looking at different output formats

From Dev

Convert DateTime from specific time zone to UTC using Nodatime?

From Dev

Unable to parse date string using mutliple formats

From Dev

How to achieve vector input/output with different operations using anonymous functions?

From Dev

Input array and different output after using PorterStemmer in php

From Dev

Using sed with two possible input formats

From Dev

Using Python and Regex to extract different formats of dates

From Dev

SimpleDateFormat.parse() - generates wrong date for different date-formats

From Dev

Parse time elapsed / time span / duration with several different formats

From Dev

Parse time elapsed / time span / duration with several different formats

From Dev

How to Extend Django DATETIME_INPUT_FORMATS Setting

From Dev

speed difference for pd.to_datetime for 2 different formats

From Dev

OpenCvSharp PCA Exception: Unsupported combination of input and output array formats

From Dev

OpenCvSharp PCA Exception: Unsupported combination of input and output array formats

From Dev

Segment stream and dump to different output file formats with ffmpeg

From Dev

Cannot parse datetime using strptime

From Dev

NodaTime: Replicating DateTime.Today

From Dev

Provide different formats based on input types in C#

Related Related

  1. 1

    Using NodaTime to parse an input and output different dateTime formats

  2. 2

    Serialize and then Deserialize DateTime using different formats

  3. 3

    Parse datetime in multiple formats

  4. 4

    Build a simple parser that is able to parse different date formats using PyParse

  5. 5

    Build a simple parser that is able to parse different date formats using PyParse

  6. 6

    Swift parse string with different formats

  7. 7

    Compare Two different formats of the DateTime

  8. 8

    How to use different NumberStyles as input(parse) and output(ToString) styles?

  9. 9

    pandas to_datetime formats date from same column in different formats

  10. 10

    Two different string formats for parsing DateTime

  11. 11

    Java or Scala fast way to parse dates with many different formats using java.time

  12. 12

    Content Negotiation when looking at different output formats

  13. 13

    Convert DateTime from specific time zone to UTC using Nodatime?

  14. 14

    Unable to parse date string using mutliple formats

  15. 15

    How to achieve vector input/output with different operations using anonymous functions?

  16. 16

    Input array and different output after using PorterStemmer in php

  17. 17

    Using sed with two possible input formats

  18. 18

    Using Python and Regex to extract different formats of dates

  19. 19

    SimpleDateFormat.parse() - generates wrong date for different date-formats

  20. 20

    Parse time elapsed / time span / duration with several different formats

  21. 21

    Parse time elapsed / time span / duration with several different formats

  22. 22

    How to Extend Django DATETIME_INPUT_FORMATS Setting

  23. 23

    speed difference for pd.to_datetime for 2 different formats

  24. 24

    OpenCvSharp PCA Exception: Unsupported combination of input and output array formats

  25. 25

    OpenCvSharp PCA Exception: Unsupported combination of input and output array formats

  26. 26

    Segment stream and dump to different output file formats with ffmpeg

  27. 27

    Cannot parse datetime using strptime

  28. 28

    NodaTime: Replicating DateTime.Today

  29. 29

    Provide different formats based on input types in C#

HotTag

Archive