Convert JavaScript Date to .NET DateTime - javascript

I getting a Date value from JavaScript to a controller in MVC and I would like to parse it to .NET format DateTime but its giving me an error such as:
The string was not recognized as a valid DateTime.
The Format of the JavaScript date is:
"Wed May 23 2012 01:40:00 GMT+0200 (W. Europe Daylight Time)"
I've tried this but its not working:
DateTime.ParseExact(begin.Substring(1, 24), "ddd MMM d yyyy HH:mm:ss", CultureInfo.InvariantCulture);
anyone can give me a sample code please? thanks!

The following parses well with the default DateTime modelbinder in a .NET MVC Controller:
var myJsDate = new Date();
var myDotNetDate = myJsDate.toISOString();

Instead of parsing a textual representation it would be more robust to construct a DateTime from a timestamp instead. To get a timestamp from a JS Date:
var msec = date.getTime();
And to convert msec (which represents a quantity of milliseconds) into a DateTime:
var date = new DateTime(1970, 1, 1, 0, 0, 0, 0); // epoch start
date = date.AddMilliseconds(msec); // you have to get this from JS of course

Here is what I did and why. I hope this Helps.
JS Date var d = new Date()
returns: Thu Nov 19 08:30:18 PST 2015
C# does not like this format so convert it to UTC:
var dc = d.toUTCString()
returns: Thu, 19 Nov 2015 16:30:18 UTC
UTC – The Worlds Time Standard is not a time zone so you need to change it to a time zone
var cr = dc.replace("UTC","GMT")
now it is ready to
Thu, 19 Nov 2015 16:30:18 GMT
In one line
var ol = d.toUTCString().replace("UTC","GMT")`
Thu, 19 Nov 2015 16:30:18 GMT
for C#
DateTime DateCreated= DateTime.Parse(ol);

You don't need any conversion: the default DateTime modelbinder in a .NET MVC Controller works fine with a JavaScript Date object.
Using Moment.js
1) .NET DateTime -> JavaScript Date
var jsDate = moment(dotNetDateTime).toDate();
2) JavaScript Date -> .NET DateTime
var dotNetDateTime = jsDate;

Related

Date format in javascript and SQL

I have a string "03/31/2017". I need to pass it as a Date to the SQL database. I tried to use new Date("03/31/2017") but it returns Thu Mar 30 2017 00:00:00 GMT-0400 (Eastern Daylight Time) Is there anyway I can keep the original date format as a data object without using momentum or anyother library. I can't use any library but jQuery.
The correct date format for Javascript is: YYYY-MM-DD
var dt = new Date('2017-03-27');
To be safer, do NOT use a string to specify the date, instead supply the parameters:
var dt = new Date(2017, 3 - 1, 27); // months are zero based

Parse Javascript date to C# DateTime, FF data is other then Chrome date

I convert javascript date to C# DateTime.
When I use firefox, JavaScript return date to my C# function:
string jsDate = "Fri Dec 05 2014 00:00:00 GMT+0100";
so, I parse it to C# DateTime using:
DateTime.TryParseExact(JsDate, "ddd MMM dd yyyy HH:mm:ss 'GMT'K", CultureInfo.InvariantCulture, DateTimeStyles.None, out Date)
When I using Chrome, js return me date in format: "Fri Dec 05 2014 00:00:00 GMT+0100 (Środkowoeuropejski czas stand.)" (
Central European standard time)
How I can parse the second one time?
Basically, you shouldn't use the default string representation from the browser. Otherwise you need to know which language it's going to use for month names etc, and you're basically fighting a losing battle.
I would strongly recommend that you format the string in a culture-neutral way when you pass it from the browser to the server - e.g. as ISO-8601 such as yyyy-MM-ddTHH:mm:ssZ. You should consider whether or not you need the time zone offset - you may just want to send it in UTC for simplicity. (If you do send the offset from UTC, you should probably parse it as a DateTimeOffset in your C# code.)
String dateString = "Fri Dec 05 2014 00:00:00 GMT+0100 (Środkowoeuropejski czas stand.)"
dateString = dateString.subStr(0,dateString.indexOf('(')-1);
DateTime.TryParseExact(dateString, "ddd MMM dd yyyy HH:mm:ss 'GMT'K", CultureInfo.InvariantCulture, DateTimeStyles.None, out Date)

Remove time difference from datetime in javascript

I'm displaying entries from an RSS feed onto my site but the date that comes with each entry is in a non-standard format, so I'm having difficulty creating a new javascript date object from it.
Is it possible to convert the following date to a date object:
Thu, 20 Feb 2014 07:52:45 -0800
and output it into this format?:
Thu, 20 Feb 2014 15:52:45
Maybe you are looking for toUTCString:
(new Date('Thu, 20 Feb 2014 07:52:45 -0800')).toUTCString()
// "Thu, 20 Feb 2014 15:52:45 GMT"
Your date string is already in a format that Date understands.
Your date is already readable to javascript so you need to do utcString of the date then replace utc part and parse as date to get the result in desired format like this:
console.log((new Date('Thu, 20 Feb 2014 07:52:45 -0800')).toUTCString().toString().replace('GMT',''));
You might find this method getTimezoneOffset() on date useful.
For example where I am:
var dateObj = new Date();
dateObj.getTimezoneOffset(); //returns -330
i.e. 330 minutes - 5:30 hrs
const today = new Date(new Date().setHours(0,0,0,0));

How to change the date string to date time with local timezone in js?

I use date picker and i get the value of the date picker as
document.getElementById("id_of_datepicker")
and when i change this to date using
new Date(document.getElementById("id_of_datepicker"));
it returns
Thu Feb 06 2014 05:30:00 GMT+0530 (IST)
and what i look for is instead of passing the dates as i do i need to send it in local timezone with 00 hours, min, secs which then has to be converted to utc. how can i do this?
please help me.
you have to split it manually and use Date.UTC for utc function like this:
var dateStr = document.getElementById("id_of_datepicker");
var dateArr = dateStr.split(/[\-T:]/);//suppose it is like 2014-06-12T00:00:00
var localTime=new Date(Date.parse(dateArr[0],dateArr[1],dateArr[2]), 0, 0, 0)
var utcTime=new Date(Date.UTC(dateArr[0],dateArr[1],dateArr[2]), 0, 0, 0)
You could try using moment.js for date/time manipulation
Suppose this the dt value returned by the date object
Thu Feb 06 2014 05:30:00 GMT+0530 (IST)
Date dt=new Date();
dat=dt.getFullYear()+'-'+dt.getMonth()+'-'+dt.getDate();
It will return date dt print in yyyy-mm-dd format ot you can change in your desired format

Can jQuery DateTime parse this date?

I have a datetime of the form:
var myDate = "2013-06-07T00:00:00.000Z"
I wish to do
jQuery.datepicker.parseDate( "yy-mm-dd", myDate);
I don't care about the time part.
I get:
"Extra/unparsed characters found in date: T00:00:00.000Z"
Best I got so far is:
myDate = myDate.replace('T00:00:00.000Z', '');
myDate = jQuery.datepicker.parseDate("yy-mm-dd", myDate).toUTCString();
Please help.
As it is ISO date format, I think you can call new Date(myDate) directly there is no need to parse it I think
var date = new Date(myDate);
If you don't care about the time part, why not simply
jQuery.datepicker.parseDate( "yy-mm-dd", myDate.split("T")[0]);
Perhaps for general DateTime handling, have a look at
moment.js
You can use split
var myDate = "2013-06-07T00:00:00.000Z";
var n=myDate.split("T");
console.log(n); // Pass the date part only to date picker
By modifying the String used to describe the format, you can get it to do this (assuming time is always zero)
var myDate = "2013-06-07T00:00:00.000Z",
d = jQuery.datepicker.parseDate("yy-mm-ddT00:00:00.000Z", myDate);
d; // Fri Jun 07 2013 00:00:00 GMT+0100 (GMT Daylight Time)
However, this will ignore the fact that Z denotes timezone UTC and instead uses local timezone (in my case BST/GMT+1). You can repair this quickly though.
d.setMinutes(d.getMinutes() - d.getTimezoneOffset()); // or use UTCMinutes
d; // Thu Jun 07 2013 01:00:00 GMT+0100 (GMT Daylight Time)
// which is now correct in terms of timezone
If you want something that will change your life, just use http://momentjs.com/... It's the equivalent of Carbon for PHP. Simple, multi-language and cross-browsers.
For your question, it goes :
moment("2013-06-07T00:00:00.000Z").format('YY-MM-DD')
Hope this helps future visitors.
What about this:
jQuery.datepicker.parseDate( "yy-mm-dd", myDate.substr(0, 10) );

Categories

Resources