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);
Related
I am trying to convert a date in format momentjs into a date from javascript native new Date().
The problem is that if I have moment(myDay).toDate(); it converts to the current date, and I want the date from myDay.
myDay looks like: "YYYY-MM-DD" => 2017-11-24 and I would like to have it with the format: Fri Nov 24 2017 20:17:11 GMT+0100 (Hora estándar romance) but I get Thu Nov 16 2017 etc...
It is possible to convert it like that way?
Don't need moment:
let [yr, mn, day] = myDay.split('-').map(Number);
// note that JS months are 0-11 not 1-12
let datestr = new Date(yr, mn - 1, dy).toString();
console.log(datestr); // "Fri Nov 24 2017 00:00:00 GMT-0500 (EST)"
you want something like this:
moment(myDay, "YYYY-MM-DD").toString();
moment().toString() Returns an english string in a similar format to JS Date's .toString().
moment().toString() // "Sat Apr 30 2016 16:59:46 GMT-0500"
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);
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
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')
I have checked this SO post: Where can I find documentation on formatting a date in JavaScript?
Also I have looked into http://home.clara.net/shotover/datetest.htm
My string is: Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)
And I want to convert it to dd-mm-yyyy format.
I tried using:
var dateString = 'Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)';
var myDate = new Date(dateString);
var final_date = myDate.getDay()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
But it gives me the result as: 1-6-2013
The getDay() value is the index of day in a week.
For Instance,
If my dateString is Thu Jun 20 2013 05:30:00 GMT+0530 (India Standard Time)
it gives output as 4-6-2013
How can I get the proper value of Day?
P.S: I tried using .toLocaleString() and creating new date object from it. But it gives the same result.
To get the day of the month use getDate():
var final_date = myDate.getDate()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
W3 schools suggests just building your days of the week array and using it:
var d=new Date();
var weekday=new Array(7);
weekday[0]="Sunday";
weekday[1]="Monday";
weekday[2]="Tuesday";
weekday[3]="Wednesday";
weekday[4]="Thursday";
weekday[5]="Friday";
weekday[6]="Saturday";
var n = weekday[d.getDay()];
Not super elegant, but usable.
var dateString = 'Mon Jun 24 2013 05:30:00 GMT+0530 (India Standard Time)';
var myDate = new Date(dateString);
var final_date = myDate.getDate()+"-"+(myDate.getMonth()+1)+"-"+myDate.getFullYear();
Replace getDay() with getDate().
The above will return the local date for each date part, use the UTC variants if you need the universal time.
I think you will have to take an Array of the days & utilize it using the received index from the getDay() method.
To get required format with given date will achieve with moment.js.
a one liner solution is
import moment from "moment";
const date = new Date();
const finalDate = moment(date).format("DD-MM-YYYY")