.Net DateTime() to Date using javascript - javascript

I am getting the Date value as DateTime from code behind to my javascript. I have to handle it in javascript only, below is the string I am currently getting from code behind to my Client side.
"Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)"
How to change this string to Date only from javascript. I want the format as "dd/mm/yyyy".

Try to use moment.js
var day = moment("Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)", "dd/mm/yyyy");

Ok.
Just got the solution.
below is the javascript code taking argument as the .Net DateTime() string
function formatTheDate(strDate) {
if (strDate != '') {
var date = new Date(Date.parse(strDate));
var formatedDate = (date.getUTCDate()) + "/" + (date.getMonth() + 1) + "/" + date.getFullYear();
return formatedDate;
}
}
fiddler : http://jsfiddle.net/xL5rdce9/

If you cannot change it server side, then start here:
alert(new Date(Date.parse("Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)")))
then according to your own answer, you can end up here:
function pad(num) {
return ("0"+num).slice(-2);
}
var myCSharpString = "Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)";
var date = new Date(Date.parse(myCSharpString));
alert(pad(date.getUTCDate()) + "/" + pad(date.getMonth() + 1) + "/" + date.getFullYear());

You can simply use MomentJS for dates.
var format = moment("Mon Oct 05 2015 13:34:29 GMT+0530 (India Standard Time)").format("DD-MM-YYYY");
alert(new Date(format));
<script src="https://cdn.jsdelivr.net/momentjs/2.10.6/moment.min.js"></script>

Related

convert date variable into particular format in javascript

I have this yest_date variable in javascript
var yest_date = "Mon Dec 12 2016 15:33:41 GMT-0800 (Pacific Standard Time)"
I want this variable 'yest_date' to be in this format and the value is.
20161212
Can someone let me know how to achieve this.
I would suggest use moment.js. It is a very good library to handle any date time related problem http://momentjs.com/
var yest_date = moment("Mon Dec 12 2016 15:33:41 GMT-0800 (Pacific Standard Time)")
console.log(yest_date.format("YYYYMMDD"))
If you don't want to add an extra library then you can use the classic string concat
let yest_date = new Date("Mon Dec 12 2016 15:33:41 GMT-0800 (Pacific Standard Time)")
console.log(`${yest_date.getFullYear()}${yest_date.getMonth() + 1}${yest_date.getDate()}`)
Simply convert your string into an actual Date, then use the Date getter methods to extract the values you want into a formatted string:
let yest_date = "Mon Dec 12 2016 15:33:41 GMT-0800 (Pacific Standard Time)"
let date = new Date(yest_date);
console.log(`${date.getFullYear()}${date.getMonth() + 1}${date.getDate()}`)
You can do the following:
const date = new Date('Mon Dec 12 2016 15:33:41 GMT-0800 (Pacific Standard Time)')
const day = date.getDate();
const month = date.getMonth() + 1;
const year = date.getFullYear();
const formattedDate = `${year}${month}${day}`;
console.log(formattedDate);

Date Formatting and filtering in angularjs

I want to convert "Mon Oct 12 2015 00:00:00 GMT+0530 (IST)" date format to "YYYY/MM/DD" in my controller.
Try to do this in this way:
var date = "Mon Oct 12 2015 00:00:00 GMT+0530 (IST)";
var newDate = $filter('date')(new Date(date), 'yyyy/MM/dd');
Angular date

convert date from 23-08-2015 00:00:00 to Tue Aug 23 2015 00:00:00 GMT+0530

I have date in 23-08-2015 00:00:00 format in my controller and pass it to view using ViewData. I want to convert this date to Tue Aug 23 2015 00:00:00 GMT+0530 format.. Is it possible with controller or can I convert it in my view using jquery?
Can anyone help me to solve this?
In javascript, if you simply create a new Date object with the string that you have, you'll get the desired format.
var testdate = new Date("23-08-2015 00:00:00");
console.log(testdate);
Output:
Tue Nov 08 2016 00:00:00 GMT+0530 (India Standard Time)
In your controller:
DateTime date = DateTime.Now;
date.ToLongDateString();
I done it by using following format:
string startDateCalendar = Convert.ToString(startDate.ToString("ddd MMM dd yyyy HH:mm:ss")) + " GMT+0530";
using JavaScript it is like so:
function parseDDMMYYYY(d){
return new Date(
d.replace(/^(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+)/,"$3-$2-$1 $4:$5:$6")
)
}
parseDDMMYYYY('23-08-2015 00:00:00')

Javascript datetime format varying on different systems

I have a method to covert date to IST which is,
function GetCurrentIST() {
var dte = new Date();
dte.setTime(dte.getTime() + (dte.getTimezoneOffset() + parseInt(UtcOffset)) * 60 * 1000);
return dte.toLocaleString();
}
another function calls it like this,
function GetStartDate(Selected) {
var StartDt = new Date(GetCurrentIST());
StartDt.setDate(StartDt.getDate() + parseInt(Selected));
StartDt.setHours(0);
StartDt.setMinutes(0);
StartDt.setSeconds(0);
return StartDt;
}
The problem I'm facing is on my machine GetCurrentIST() returns date properly.
Eg. "10/12/2014, 12:26:37 PM"
and the StartDt I get as : Dec 10 2014 12:37:32 GMT+0530 (India Standard Time)
But on my friends machine GetCurrentIST() returns date as : "10/12/2014 12:28:40"
and StartDt as : Sun Oct 12 2014 12:29:09 GMT+0530 (India Standard Time)
Is this problem related to datetime format or something.
does anyone have any simple solution for this problem?

Converting a date string into UTC+0530 format using javascript

I have a date in the format 14-Feb-2011, but I want to convert it into the format Mon Feb 14 10:13:50 UTC+0530 2011. How Can I achieve this?
Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.
I tried this code and it returned proper date (In Indian Locale)
var d=Date.parse("14,Feb,2011");
document.write(new Date(d));
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time) .
Here's an example of converting between different time zones.
<html>
<body>
<script type="text/javascript">
//Set you offset here like +5.5 for IST
var offsetIST = 5.5;
//Set you offset here like -8 for PST
var offsetPST = -8;
//Create a new date from the Given string
var d=new Date(Date.parse("14,Feb,2011"));
//To convert to UTC datetime by subtracting the current Timezone offset
var utcdate = new Date(d.getTime() + (d.getTimezoneOffset()*60000));
//Then cinver the UTS date to the required time zone offset like back to 5.5 for IST
var istdate = new Date(utcdate.getTime() - ((-offsetIST*60)*60000));
//Then cinver the UTS date to the required time zone offset like back to -8 for PST (Canada US)
var pstdate= new Date(utcdate.getTime() - ((-offsetPST*60)*60000));
document.write(d);
document.write("<br/>");
document.write(utcdate);
document.write("<br/>");
document.write(istdate);
document.write("<br/>");
document.write(pstdate);
</script>
</body>
</html>
Output:
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 18:30:00 GMT+0530 (India Standard Time)
Mon Feb 14 2011 00:00:00 GMT+0530 (India Standard Time)
Sun Feb 13 2011 10:30:00 GMT+0530 (India Standard Time)
Its writing IST every where because new Date() always show date as local timezone (which is IST for me) but above datetime are actually Original, UTC, IST, PST respectively.
var d = new Date("14-Feb-2011");
this will give an output of
Mon Feb 14 2011 00:00:00 GMT-0500 (Eastern Standard Time)

Categories

Resources