When I retrieve the date value from c# it will be in the following format:
/Date(1406844000000)/
then in JavaScript i make this code :
var date1 = new Date(parseInt(ret.startDate.substr(6)));
and when I display this value in label it display as follow:
Fri Aug 01 2014 00:00:00 GMT+0200 (Egypt Standard Time)
and I need only
Fri Aug 01 2014
How extract only date in java script from above value?
You can convert the date to a string.
date1.toString().substr(0, 15)
date1.toDateString()
or if you want it localized:
date1.toLocaleDateString()
Related
I am using react-native-calendar-picker. Initially when my app data loads, if there isn't a date stored that was picked, I assign the specific array with new Date() which returns something like this: Thu Feb 18 2016 22:58:12 GMT-0700 (MST). Once the user updates the date I store the new date in React Native's AsyncStorage which has a value something like this: Mon Feb 29 2016 00:00:00 GMT-0700 (MST)
However, once the app reloads the date value is returned like this: "2016-02-29T07:00:00.000Z". The react-native-calendar-picker uses the date formatted like: Mon Feb 29 2016 00:00:00 GMT-0700 (MST)
Is there a way to get the date formatted back to this way?
Try,
String formatted = new Date("2016-02-29T07:00:00.000Z").toString();
Precisely be aware of data types,
new Date() would return a Date object. (2016-02-19T13:42:17.975Z)
new Date().toString() would return a formatted date string. (Fri Feb 19 2016 19:12:17 GMT+0530 (IST))
I have code:
var date = '2014/01/01';
date = new Date(Date.parse(date));
date = date.toString();
I get string like this - "Wed Jan 01 2014 00:00:00 GMT+0400 (MSK)". It's good, but in my case i need to get string without "(MSK)"
I need - "Wed Jan 01 2014 00:00:00 GMT+0400" only!
How can i do it?
Thanks in advance!
If its a string, just do something like:
date = date.toString();
date = date.replace(/\([A-Z]+\)$/,"");
You could also change the format by passing in a new value to date.toString("%format%"), as per:
http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.110%29.aspx
..but for what you need, simply doing the replacement would be the easiest :)
How can I display a date to behave like that day regardless of the users time-zone?
>>> new Date('2013-09-17')
Date {Mon Sep 16 2013 20:00:00 GMT-0400 (Eastern Standard Time)}
I'm trying to use the jquery datepicker formater, however when I pass the date object it's off by a day.
How can I make sure that the users timezone is disregarded?
OK, I split the string and passed them as parameters for my expected result.
>>> d = '2013-09-17'.split('-'); new Date(d[0],d[1]-1,d[2]);
Date {Thu Oct 17 2013 00:00:00 GMT-0400 (Eastern Standard Time)}
This question already has answers here:
Where can I find documentation on formatting a date in JavaScript?
(39 answers)
Closed 8 years ago.
I am working towards formatting my date and time from MON APR 08 2013 00:00:00 GMT-0400 (EASTERN DAYLIGHT TIME) to look like MON APR 08 2013 01:01:01. Although I am having no luck with everything I have tried. Can someone shed a little light. Below is the last piece of code I have tried. Thanks.
var date = new Date(parseInt(data[0].published.substr(6)));
var time = new Date(date.toLocaleDateString());
If you can, the best practice would probably be to format the date server-side, or at least present a more universally useful date (like a UNIX timestamp) instead of the formatted string.
However, if changing the server-side output is not an option, you can use the javascript date object. I see you've tried that, but you're not using the date object's constructor properly:
var dateString = 'MON APR 08 2013 00:00:00 GMT-0400 (EASTERN DAYLIGHT TIME)';
var dte = new Date(dateString);
document.write(dte.toDateString()); // output: Mon Apr 08 2013
Try it: http://jsfiddle.net/BvLkq/
If you need to reconstruct the time, you can use toLocaleDateString (docs) to pass a locale or format string, or you can build one up by hand using the getHours() (etc) functions .
Documentation
Date object on MDN - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Just use a simple regex.
var str = 'MON APR 08 2013 00:00:00 GMT-0400 (EASTERN DAYLIGHT TIME)';
console.log(str.replace(/(.*\d{2}\:\d{2}\:\d{2}).*$/, '$1'));
// outputs MON APR 08 2013 00:00:00
I'm getting incorrect dates in Chrome...
My code looks like this..
Title contains "2013-06-14T00:00:00", it was a DateTime in C# returned from WebAPI
As you can see here on both browsers..
When I add it to a new javascript date like this..
var dt = new Date(title)
I get different dates in different browsers...
Example - http://jsfiddle.net/RvUSq/
Looks like Firefox is assuming this datetime format without timezone is local time and Chrome/Webkit is assuming it's UTC.
If the datetime returned from the api is UTC, simply append a "Z" to the end of the string, so it becomes "2013-06-14T00:00:00Z", which indicates the time is in UTC, then you will get the same result in the two browsers.
Convert timestamp to ISO 8601 formatted string in C#, for e.g
var title = "14 JUN 2013 00:00:00" // printed from C#
Then use Date constructor
var date = new Date(title);
If you don't specify timezone the local timezone in the client machine will be set to the given time. If you specify the timezone, needed calculations will be done to convert the date to local timezone.
var title = "14 JUN 2013 00:00:00";
var date = new Date(title); // Fri Jun 14 2013 00:00:00 GMT+0530 (IST)
var title = "14 JUN 2013 00:00:00 GMT";
var date = new Date(title); // Fri Jun 14 2013 05:30:00 GMT+0530 (IST)
var title = "14 JUN 2013 00:00:00 GMT-0400";
var date = new Date(title); // Fri Jun 14 2013 09:30:00 GMT+0530 (IST)
ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse