convert date variable into particular format in javascript - 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);

Related

how convert a new string date into a date with javascript

I have a string of new date value. How can I covert it into my expected output using vanilla JavaScript.
let str = "Sun May 01 2022 20:30:20 GMT+0600 (Bangladesh Standard Time)"
expected output: May 01, 2022
var myDate="Sun May 01 2022 20:30:20 GMT+0600 (Bangladesh Standard Time)";
var dateArrays=myDate.split(" ");
var newDate=dateArrays[1]+" "+dateArrays[2]+", "+dateArrays[3]
console.log(newDate);

The wrong date format in Javascript

I am using luxon library to convert the time:
const DateTime = luxon.DateTime;
console.log(DateTime.local('Sun Jan 23 2003 00:00:00 GMT+0200 (Eastern European Standard Time)').toISODate())
<script src="https://cdn.jsdelivr.net/npm/luxon#2.3.1/build/global/luxon.min.js"></script>
I expect to get this format: 2003-04-23 Why i get null and how to get the expected format using luxon?
You have the date format wrong, Luxon’s format that belongs in .local is year?, month, day, hour, minute, second, millisecond
Example:
const DateTime = luxon.DateTime;
console.log(
DateTime.local(2003, 1, 23, 17, 36) .toISODate())
<script src="https://cdn.jsdelivr.net/npm/luxon#2.3.1/build/global/luxon.min.js"></script>
For your case you can use the code below to get what you are looking for.
const DateTime = luxon.DateTime;
const date = new Date("Sun Jan 23 2003 00:00:00 GMT+0200 (Eastern European Standard Time)")
const dat = DateTime.fromJSDate(date)
console.log(dat.toFormat('MM-dd-yyyy'))
<script src="https://cdn.jsdelivr.net/npm/luxon#2.3.1/build/global/luxon.min.js"></script>
can be done with pure date
var date =new Date("Sun Jan 23 2003 00:00:00 GMT+0200 (Eastern European Standard Time)")
const DateTime = luxon.DateTime;
var dat = DateTime.fromJSDate(date)
console.log(dat.toString().split("T")[0])
<script src="https://cdn.jsdelivr.net/npm/luxon#2.3.1/build/global/luxon.min.js"></script>

Transform date to wire format (YYYY-MM-DD) in javascript

Having this input: Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time), is there a way to format it as YYYY-MM-DD, to it will become 2021-02-03?
Try this:
const date = moment(new Date('Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)')).format('YYYY-MM-DD');
console.log(date);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
Parsing a timestamp to a date to reformat it may well produce incorrect results if the timestamp includes an offset or timezone. Given "Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)", for a user with an offset of +1 or less the date will be 2 Feb, not 3 Feb.
The most reliable method is to reformat the string, e.g.
let timestamp = "Wed Feb 03 2021 00:00:00 GMT+0200 (Eastern European Standard Time)";
let months = [,'Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
let pad = n=>('0'+n).slice(-2);
let p = timestamp.split(' ');
console.log(`${p[3]}-${pad(months.indexOf(p[1]))}-${p[2]}`);

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')

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