Getting date format in C# identical to javascript date function - c#

I'm assigning the date to the variable in javascript.
var myDate = new Date(y, m, 1)
So I get the date in myDate as: Fri Mar 01 2013 00:00:00 GMT+0530 (India Standard Time)
I need to format the date string in C sharp in the same manner.
I tried something like this:
string.Format("{0:yyyy-MM-dd hh:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"))
It gives me: "2013-03-12 01:31:49 GMT +05:30"
So it's not the exact format I want. Any help...

This should work
System.DateTime.Now.ToString("ddd MMM dd yyyy HH:mm:ss \"GMT\"K")
returns "Tue Mar 12 2013 14:01:38 GMT+05:30"

What you want is the following which will give you exactly the same as JavaScript!
string.Format("{0:ddd MMM dd yyyy hh:mm:ss \"GMT\"K} ({1})", dt.ToLocalTime(), TimeZoneInfo.Local.StandardName)

There's probably a more proper way, but in this case the initial part of your format string is just off:
ddd MMM dd yyyy
string.Format("{0:ddd MMM dd yyyy hh:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"))

string.Format("{0:ddd MMM dd yyyy hh:mm:ss} GMT {1}", dt.ToLocalTime(), dt.ToLocalTime().ToString("%K"))

You format string is wrong.
Should be something like:
"{0:ddd MMM dd yyyy hh:mm:ss} GMT{1}"

Why not just use
dt.ToString("ddd MMM dd yyyy HH':'mm':'ss 'GMT'K");
Should give you
Fri Mar 01 2013 00:00:00 GMT+0530

Try This Code>>
DateTime dt = DateTime.Now;
String.Format("{0:dd-MM-yyyy}", dt);

Related

c# date time parse from Fri Feb 21 23:07:58 +0000 2020

I have the following date
string dateTimeText = #"Fri Feb 21 23:07:58 +0000 2020";
I want to parse it:
DateTime.ParseExact(dateTimeText, "D M dd HH:mm:ss +ssss yyyy", new CultureInfo("en-US"));
this implementation throws exception. Thanks
Well, if +ssss (+0000) stands for TimeZone (so +0000 means GMT) the pattern is
"ddd MMM dd HH:mm:ss zzzz yyyy"
I.E.
string dateTimeText = "Fri Feb 21 23:07:58 +0000 2020";
var result = DateTime.ParseExact(
dateTimeText,
#"ddd MMM dd HH:mm:ss zzzz yyyy",
CultureInfo.GetCultureInfo("en-US"));
In case +ssss and (an corresponding +0000) are fractions of seconds the pattern will be
"ddd MMM dd HH:mm:ss' +'FFFF yyyy"

Parse DateTime from string c#

I have date that I get from incoming API call: Wed, 6 Mar 2019 14:39:49 +0300
I need to parse this string to DateTime. For this I'm using the following code:
DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300",
new string[] { "ddd, dd MMM yyyy HH:mm:ss zzzz" },
CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
But as a result I have error:
String 'Wed, 6 Mar 2019 14:39:49 +0300' was not recognized as a valid
DateTime.
What am I doing wrong? How can I resolve this?
I see 2 things;
You should use d specifier instead of dd specifier since your single digit day number does not have a leading zero.
There is no zzzz as a custom format specifier. You should use zzz specifier instead.
DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300",
new string[] { "ddd, d MMM yyyy HH:mm:ss zzz" },
CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
But honestly, if your strings have a UTC Offset value, I would suggest parse it to DateTimeOffset instead since a DateTime instance does not have offset part and using zzz specifiers is not recomended as stated on MSDN.
With DateTime values, the "zzz" custom format specifier represents the
signed offset of the local operating system's time zone from UTC,
measured in hours and minutes. It does not reflect the value of an
instance's DateTime.Kind property. For this reason, the "zzz" format
specifier is not recommended for use with DateTime values.
To parse DateTimeOffset,
DateTimeOffset.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300",
new string[] { "ddd, d MMM yyyy HH:mm:ss zzz" },
CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
Now you can use it's .DateTime and/or .Offset properties separately if you want.
Change "ddd, dd MMM yyyy HH:mm:ss zzzz" to "ddd, d MMM yyyy HH:mm:ss zzzz"
DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300",
new string[] { "ddd, d MMM yyyy HH:mm:ss zzzz" },
CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
You might be looking for
DateTime myDate = DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300",
"ddd, d MMM yyyy HH:mm:ss zzz",
CultureInfo.InvariantCulture,
DateTimeStyles.AdjustToUniversal);
Refer: https://stackoverflow.com/a/10426999/4373895 This would also help you.Thanks.
Datetime.ParseExact function just like below code withh help you. One thing needs to be changed is instead of dd MMM yyyy use d MMM yyyy.
DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", "ddd, d MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
d MMM yyyy instead of ddd MMM yyyy
date = DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", "ddd, d MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
The format for the UTC offset is zzz not zzzz and expects it to have a colon (:) to separate the hours from the minutes (such as +03:00). As well as the dd is for leading zero day of month but you have a single digit for the day of month. In that case use d.
DateTime time = Convert.ToDateTime("Wed, 6 Mar 2019 14:39:49 +0300");
string ti = time.ToString(#"ddd, dd MMM yyyy HH:mm:ss zzzz");

Cannot parse string to date with tryparse exact

I am reading a string from a log file with a date value like this:
Thu Oct 06 15:38:45 2016
Obviously
DateTime.TryParse()...
will not handle that. So, I tried using
DateTime.TryParseExact()
like this:
string szDateFormat = "ddd MMM yy HH:mm:ss yyyy";
string szTest = "Thu Oct 06 15:38:45 2016";
DateTime dd;
DateTime.TryParse(szTest, out dd);
CultureInfo current = CultureInfo.CurrentCulture;
CultureInfo newculture = new CultureInfo("en-US");
Console.WriteLine(current);
Console.WriteLine(newculture);
Console.WriteLine(String.Format(" Test: {0}", szTest));
Console.WriteLine(String.Format("Format: {0}", szDateFormat));
Console.WriteLine(String.Format(" dd: {0:ddd MMM yy HH:mm:ss yyyy}", dd));
Console.WriteLine("");
DateTime.TryParseExact(szTest, szDateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dd);
Console.WriteLine(String.Format(" Test: {0}", szTest));
Console.WriteLine(String.Format("Format: {0}", szDateFormat));
Console.WriteLine(String.Format(" dd: {0:ddd MMM yy HH:mm:ss yyyy}", dd));
Console.WriteLine("");
And the result is this:
en-US
en-US
Test: Thu Oct 06 15:38:45 2016
Format: ddd MMM yy HH:mm:ss yyyy
dd: Mon Jan 01 00:00:00 0001
Test: Thu Oct 06 15:38:45 2016
Format: ddd MMM yy HH:mm:ss yyyy
dd: Mon Jan 01 00:00:00 0001
In a previous iteration I used
CultureInfo.CurrentCulture
as well with no change.
Any help would be appreciated.
Thanks,
John
You are attempting to parse the numeric day (06) with yy when you should be using dd:
ddd MMM dd HH:mm:ss yyyy
I think this is the problem:
string szDateFormat = "ddd MMM yy HH:mm:ss yyyy";
string szTest = "Thu Oct 06 15:38:45 2016";
the format should be:
ddd MMM dd HH:mm:ss yyyy
if you want to parse:
Thursday 06.10.2016 15:38:45
The third parameter yyshould be dd as it will conflict with the last parameter yyyy. yy is pointing to 2006, but the yyyy refers to 2016. This will create a confusion for the parser.
I think that your date format is incorrect "ddd MMM yy HH:mm:ss yyyy" has to be
"ddd MMM dd HH:mm:ss yyyy"
My two cents:
"dd" is never set, since it is never parsed propery in the first place so " dd: Mon Jan 01 00:00:00 0001" is not really meaningful.
If your input format is always the same, pre-parse it for instance put the last 4 chars - year- in the start of the string)

Convert a specific type of sting in Date Time

I have to convert the below string in DateTime. I have used following code for that but it was not working.
DateTime created = DateTime.ParseExact("Sun Feb 23 2014 00:00:00 GMT+0550", "ddd mmm d yyyy HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture);
My string to which I have to convert in Date Time is--
Sun Feb 23 2014 00:00:00 GMT+0550
You need uppercase month and 'GMT'zzz
DateTime created = DateTime.ParseExact("Sun Feb 23 2014 00:00:00 GMT+0550"
, "ddd MMM dd yyyy HH:mm:ss 'GMT'zzz"
, System.Globalization.CultureInfo.InvariantCulture);
I would parse this as a DateTimeOffset instead of a DateTime - after all, that's the data you've been given. Assuming it's always specified using GMT+... you can use a format string of "ddd MMM d yyyy HH:mm:ss 'GMT'zzz". In particular, note:
MMM is abbreviated month name, not mmm
'GMT' will always match the letters 'GMT'
zzz is a UTC offset including minutes. It would be formatted with a colon, but apparently it's permissive enough without the colon being specified.
Sample code:
using System;
using System.Globalization;
class Test
{
static void Main()
{
var dto = DateTimeOffset.ParseExact
("Sun Feb 23 2014 00:00:00 GMT+0550",
"ddd MMM d yyyy HH:mm:ss 'GMT'zzz",
CultureInfo.InvariantCulture);
Console.WriteLine(dto); // 23/02/2014 00:00:00 +05:50
}
}
Try This:
DateTime created = DateTime.ParseExact("Sun Feb 23 2014 00:00:00 GMT+0550",
"ddd MMM d yyyy HH:mm:ss 'GMT'zzz",
System.Globalization.CultureInfo.InvariantCulture);

datetime conversion c#

how would you convert this date into c# Datetime object?
Mon Mar 16 14:21:27 +0000 2009
i tried parseexact with format = "ddd MMM dd hh:mm:ss zzzz yyyy" but it didn't work.
what did i do wrong?
For 24-hour hours, you want HH, not hh:
DateTime when = DateTime.ParseExact("Mon Mar 16 14:21:27 +0000 2009",
"ddd MMM dd HH:mm:ss zzzz yyyy",
CultureInfo.InvariantCulture);

Categories