How to get correct time on client side javascript? - c#

Currently, I have a datetime field in one of my sqlserver table.
And I Query (SELECT Column1, myDateField FROM MyTimeTable BETWEEN #startDate AND #endDate).
NOW I got Column1, myDateField at C# code side using Entity framework.
Problem
Then I'm passing these dates to client side as json. Now, asp.net converts date to "\/Date(1410588000000)\/" format. (ie date '19-9-2014' to 1410588000000). When I create date with this ticks (From 1970 ticks of javascript) it will add extra 5.00 hours (server timezone is -5.00). This happens because while converting datetime to "\/Date(xxxxxxxxx)\/" format, .net consider the date in database (ie, now a c# datetime using entity framework) is local (-5.00). So it creates ticks for +5.00 hours. How can I convince .net that the time is in utc?

Please convert your DATE TIME to any fixed specific format and than follow it every where,
So it will help you to maintain the consistency.
Write query like below
SELECT CONVERT(VARCHAR(30),GETDATE(),113) AS DateConvert;
Format will be "dd MMM yyyy hh:mm:ss:zzz"
this will send exact date time format , which will be converted easily.
Hope this will help you.
Now we can use DateTime.ParseExact to convert received string (Date) to DateTime
C# code
String dateString = "01 Jun 2008 08:30:06:00";
CultureInfo provider = CultureInfo.InvariantCulture;
String format = "dd MMM yyyy hh:mm:ss:zzz";
try {
DateTime result = DateTime.ParseExact(dateString, format, provider);
}
Thanks

Var date = new Date();
now the date have the value of client's time.
alert(new Date());

Related

Format date using DateTime.ToString() based on TimeZone

I read the following question Creating a DateTime in a specific Time Zone in c# and was able to create a DateTime with TimeZone information. But I need to convert the DateTime to string value based on TimeZone.
E.g. I've set the TimeZone as India Standard Time and created a DateTime, when I tried to convert to string using ToString() instead of 13/12/2019 4:00:00 PM, I am getting 12/13/2019 4:00:00 PM. Since I've set the TimeZone as India Standard Time, I would like to display the date in India Format (dd/mm/yyyy) rather than mm/dd/yyyy.
So, how do I format the date based on TimeZone in C#?
Edit: I completely understand that Format and Timezone are different things. But I need to format the DateTime to match user's geography which I can identify using his timezone provided as input.
If you only want a string representation of the DateTime that matches a specific culture you can use the DateTime.ToString(IFormatProvider) overload to specify the target culture you want to use. The date will be formatted accordingly. If you want to format your date and time you want to do this based on the culture of the user and not based on the timezone. People in Kongo and Germany share the same timezone but are formatting their date and time differently.
var myDate = DateTime.Now();
var myDateString = myDate.ToString(new CultureInfo("fr-FR"));
would print the date and time in a french format for example.
You can also format your DateTime with a custom format:
var myDate = DateTime.Now;
var myDateString = myDate.ToString("dd/MM/yyyy hh:mm");
References:
- DateTime.ToString(IFormatProvider)
- DateTime.ToString(string)
- CultureInfo
- Date and time formatting

Not reading CSV correctly when any date field is there

csv file data -
DB PID Date1 Date2
--------------------------------
DB1 561 06-04-2015 06-05-2015
DB2 538 06-04-2015 08-21-2015
DB3 722 06-04-2015 08-21-2015
Here date format is stated as MM-DD-YYYY. Problem is while reading Date2 columns date then it's reading date as DD-MM-YYYY. That's why in 08-21-2015 '08' becomes DD and '21' becomes month and got validation error.
Tried to solve the code :
Date2Val = Convert.ToDateTime(TestContext.DataRow["Date2"]).ToString("dd-MM-yyyy 00:00:00.000");
It works for Date1 column but not for Date2.
Convert.ToDateTime uses standard date and time format of your CurrentCulture.
That means your CurrentCulture has dd-MM-yyyy as a standard date and time format, not MM-dd-yyyy.
You can explicitly specify your format with custom date and time specifiers in DateTime.ParseExact method like;
Date2Val = DateTime.ParseExact(TestContext.DataRow["Date2"].ToString(),
"MM-dd-yyyy",
CultureInfo.InvariantCulture)
.ToString("dd-MM-yyyy 00:00:00.000");
You need to explicitly state the format for the date value, as DateTime.Parse always uses the date format that's configured in your system's culture information.
The following uses the format you need:
Date2Val = DateTime.ParseExact(TestContext.DataRow["Date2"].ToString(), "MM-dd-yyyy", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy 00:00:00.000");
After much ado you just told us there's not only a date value in Date2, but also a time part. In that case you need to use this:
Date2Val = DateTime.ParseExact(TestContext.DataRow["Date2"].ToString(), "MM-dd-yyyy HH:mm:ss", CultureInfo.InvariantCulture).ToString("dd-MM-yyyy 00:00:00.000");
Perhaps force the data to strings, to prevent the auto-conversion and then in the code parse it to a DateTime.
I put double qoutes around the data top force it as text. That gives back strings, without removing the quotes.
columnInt, columnString
1, "11.12.89"
2, "12.12.89"

Convert DateTime to string according to Oracle Format Mask

There is an Oracle DB API function which expects as an input a string representing date (or in other cases number) which is formatted by the provided format mask.
Before I went and implemented Oracle Format Mask parser I thought I would ask whether there is a .net built-in or third party way of converting DateTime .net object to string using Oracle Format Mask?
Update
Examples:
DateTime date = DateTime.Now;
string convertedDate = ConvertDateTimeToOracleFormattedString (date, "Month DD, YYYY"); // should produce: March 21, 2014
convertedDate = ConvertDateTimeToOracleFormattedString (date, "CC BC"); // should produce: 21
// any other valid oracle format combination: http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#i34924
My current idea is to parse provided Oracle Format Mask into equivalent .net custom date and time format string: http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx and use it when converting .net DateTime to string: DateTime.Now.ToString ("equivalentDotNetFormat");. The equivalent .net format of "Month DD, YYYY" would be "MMMM dd, yyyy". Unless there is a quicker approach.
Could you please take a look at ODP .NET?
I believe it has the necessary classes to provide you the functionality you desire.

converting a string to date in the exact manner

I have a string "11 Jan 2011" which I want to convert to the datatype date (i.e 11 Jan 2011).
I have tried all resources about datetime.parse, datetime.parse exact but all these things gives me the same output 2011/01/11 12:00:00 AM. I really don't understand this behaviour. I tried the following:
1.DateTime date = DateTime.Parse("11 Jan 2011");
2.DateTime date = DateTime.ParseExact("11 Jan 2011" , #"dd MMM yyyy", System.Globalization.CultureInfo.InvariantCulture);
parsing and displaying are not the same thing
you parse the original string to a DateTime object but display results using Date/Time format strings
Both your calls are correct.
A DateTime structure preserves no information about formatting; it just represents the raw date and time.
What you need to do is ensure that when you display your date, you do so in the correct format - e.g. by calling string displayString = date.ToString("dd MMM yyyy");

DateTime Format like HH:mm 24 Hours without AM/PM

I was searching here about converting a string like "16:20" to a DateTime type without losing the format, I said I dont want to add dd/MM/yyy or seconds or AM/PM, because db just accept this format.
I tried with Cultures yet
Thanks in Advance
Just give a date format to your dateTime.
string DateFormat = "yyyy MM d " this willl give you the year month and day. after continuing;
string DateFormat = "yyyy MM d HH:mm:ss " in here the Capital H will give you the 24 hours time format and lowerCase "h" will give you the 12 hours time format...
when you give the Dateformat as a string you can do whatever you want with date and time.
string DateFormat = "yyyyMMdHHmmss";
string date = DateTime.Now.ToStrign(DateFormat);
OR
Console.writeline(DateTime.Now.ToStrign(DateFormat));
OUTPUT:
20120823132544
All DateTime objects must have a date and a time.
If you want just the time, use TimeSpan:
TimeSpan span = TimeSpan.Parse("16:20");
If you want a DateTime, add that time to the min value:
TimeSpan span = TimeSpan.Parse("16.20");
DateTime dt = DateTime.MinValue.Add(span);
// will get you 1/1/1900 4:20 PM which can be formatted with .ToString("HH:mm") for 24 hour formatting
DateTime.Now.ToString("hh:mm") - If it's C#.
Oh. Only read the header.
DateTime dt = new DateTime(2008, 12, 11, Convert.ToInt32("16"), Convert.ToInt32("32"), 0);
what do you mean by "losing the format".
if you convert it to a DateTime type, then the DateTime object will have dd/mm/yy and other properties. depending on how you plan to use the object, you can "recover" your original settings, by formatting the string output like this: DT.ToString("HH:mm");
Since you don't stipulate which DBMS you are using, it is hard to know which answer will help you. If you use IBM Informix Dynamic Server, you would simply use the data type 'DATETIME HOUR TO MINUTE', which will record values in the 24 hour clock.
DateTime.Parse("16:20")
I want to address this part of your question:
without losing the format
A database will generally store all datetime values in a standard common format that's not even human readable. If you use a datetime column the original format is destroyed.
However, when you retrieve the value you cast it back to any format you want. If you want HH:mm you can get it.

Categories

Resources