How to split string of dates and convert into UTC dates using linq

Anil Purswani

I have comma separated string containing date and want to convert it in UTC.

I wrote this code:

daterange1.Select(t => t.Split(',').Select(r => r.Replace(r, TimeZoneInfo.ConvertTimeToUtc(DateTime.Parse(r), selectedTimeZone).ToString())));

but it just convert first element and not all the elements.

Any suggestions?

EDIT

daterange = "2016-02-15 17:30:00,2016-02-15 18:00:00;2016-02-16 17:30:00,2016-02-16 18:00:00";

var daterange1 = dateRange.Split(';');

daterange1.Select(t => t.Split(',').Select(r => r.Replace(r, TimeZoneInfo.ConvertTimeToUtc(DateTime.Parse(r), selectedTimeZone).ToString())));

each comma separated string contain startdate and enddate.

T.S.

this will do it - tested

var dates = "2016-02-15 17:30:00,2016-02-15 18:00:00,2016-02-16 17:30:00,2016-02-16 18:00:00";
DateTime[] utcDates = dates.Split(',').Select(d => DateTime.Parse(d)).Select(dt => dt.ToUniversalTime()).ToArray();

foreach (var d in utcDates)
{
       Console.WriteLine(d);
}

@mxmlc pointed out that my answer is not exactly the answer. so, here is another variation that should answer this for sure. It contains comma-separated start/end and semicolon separated date sets

var dates = "2016-02-15 17:30:00,2016-02-15 18:00:00;2016-02-16 17:30:00,2016-02-16 18:00:00";

// prepare results
// Legend: se = start/end; sed = stard-end date
var utcDates = dates.Split(';').
               Select(se => se.Split(',')).
               Select(se => new DateTime[]{ 
                                  DateTime.Parse(se[0]).ToUniversalTime(), 
                                  DateTime.Parse(se[1]).ToUniversalTime()});

// print out results                
utcDates.Select(sed => string.Format("Start Date: {0}; End Date: {1}", sed[0], sed[1])).
         ToList().
         ForEach(r => Console.WriteLine(r));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Git: show dates in UTC

From Dev

PHP MYSQL convert timezone aware dates to UTC

From Dev

How to split a string using LINQ

From Dev

Convert a string of dates with missing values

From Dev

SQLAlchemy How to load dates with timezone=UTC (dates stored without timezone)

From Dev

Issues with comparason of UTC dates

From Dev

Using LINQ to search between two Dates and Times

From Dev

How to Convert Date values in SAS into Regular Dates Using SSIS

From Dev

How to convert an array of UTC dates into milliseconds using JS/Jquery

From Dev

Compare two sql dates using Linq

From Dev

LINQ join using dates not working

From Dev

How does Elasticsearch convert dates to JSON string representations?

From Dev

How to split list of dates into weeks

From Dev

How to display dates as a string?

From Dev

Spring boot + Jackson - Always convert dates to UTC

From Dev

How to convert Multiple Dates in BeanWrapperFieldSetMapper?

From Dev

How to deal with the timezone issue when storing dates in utc using mongod?

From Dev

Git: show dates in UTC

From Dev

How to compare string dates?

From Dev

Convert a string of dates with missing values

From Dev

How to Convert Date values in SAS into Regular Dates Using SSIS

From Dev

Ignore UTC in javascript dates

From Dev

Convert an array of string dates to timestamps

From Dev

How to convert file dates to a particular format when using 'ls'

From Dev

How to sum between dates in a DataTable with multi conditions using LINQ?

From Dev

Using awk, how to convert dates to week and quarter?

From Dev

Convert string dates in java

From Dev

How to convert the difference between dates in any human format using Ruby?

From Dev

Split a list or ordered dates into weeks using linq

Related Related

  1. 1

    Git: show dates in UTC

  2. 2

    PHP MYSQL convert timezone aware dates to UTC

  3. 3

    How to split a string using LINQ

  4. 4

    Convert a string of dates with missing values

  5. 5

    SQLAlchemy How to load dates with timezone=UTC (dates stored without timezone)

  6. 6

    Issues with comparason of UTC dates

  7. 7

    Using LINQ to search between two Dates and Times

  8. 8

    How to Convert Date values in SAS into Regular Dates Using SSIS

  9. 9

    How to convert an array of UTC dates into milliseconds using JS/Jquery

  10. 10

    Compare two sql dates using Linq

  11. 11

    LINQ join using dates not working

  12. 12

    How does Elasticsearch convert dates to JSON string representations?

  13. 13

    How to split list of dates into weeks

  14. 14

    How to display dates as a string?

  15. 15

    Spring boot + Jackson - Always convert dates to UTC

  16. 16

    How to convert Multiple Dates in BeanWrapperFieldSetMapper?

  17. 17

    How to deal with the timezone issue when storing dates in utc using mongod?

  18. 18

    Git: show dates in UTC

  19. 19

    How to compare string dates?

  20. 20

    Convert a string of dates with missing values

  21. 21

    How to Convert Date values in SAS into Regular Dates Using SSIS

  22. 22

    Ignore UTC in javascript dates

  23. 23

    Convert an array of string dates to timestamps

  24. 24

    How to convert file dates to a particular format when using 'ls'

  25. 25

    How to sum between dates in a DataTable with multi conditions using LINQ?

  26. 26

    Using awk, how to convert dates to week and quarter?

  27. 27

    Convert string dates in java

  28. 28

    How to convert the difference between dates in any human format using Ruby?

  29. 29

    Split a list or ordered dates into weeks using linq

HotTag

Archive