JavaScript: Convert "10/30/2015 2:43pm" to a Date object - javascript

As the title suggests, I need to know how to turn strings in the format of "10/30/2015 2:43pm" into JavaScript Date objects. The string comes from Datepair (http://jonthornton.github.io/Datepair.js/) and I need to insert it into Mongo DB as a date. I've got Moment.js too if that's useful in this situation.
Thanks!

Just try with:
new Date("10/30/2015 2:43pm".replace(/([ap]m)$/, " $1"))

Using moment, just provide the format as second parameter:
moment("10/30/2015 2:43pm", "MM/DD/YYYY hh:mma");

Related

How to convert string into date?

in my application I want to show the last login of the user. From my backend I get this string: "lastLogin":"2022-02-22T06:02:53.585764600Z". In my frontend, I display it with:
<label class="lastVisitLabel">Last visit {{$store.state.user.lastLogin}}</label>
How can I format this string to a date type so I can use methods like .getHours() usw...
// Just pass string to new Date method
const customDate = new Date("2022-02-22T06:02:53.585764600Z");
// Test
console.log(customDate.getHours());
I already found the answer for my problem,
{{Date($store.state.user.lastLogin)}}
Just use this to cast the string inside the brackets
A possible solution is that you can pass the string date from the backend into a Date object. And only then you will be able to use Date methods.
or in your case
{{Date{$store.state.user.lastLogin}}
Please refer to the attached code as a reference.
//Value from DB
const strDate = "2022-02-22T06:02:53.585764600Z";
//Parse the date
const parsedDate = new Date(strDate);
document.querySelector("#display-date").textContent = parsedDate.getHours();
<h1 id="display-date"></h1>
MDN Date docs: Date
W3schools Date Objects

Vue JS - Retrieve date only

I'm making an api call which retrieves a set of objects. One of the objects returns a date and time together like this:
createdAt: "2020-11-04 09:48:32"
This is where I display the date and time:
<template v-for="item in collectedTrash">
<BeforeAndAfter v-if="item.isPresented === 1"
:key="item.username"
:avatarUrl="require('#/assets/img/images/img_stats_km#2x.png')"
:imageBefore="getImageUrl(item.imageUrlBefore)"
:imageAfter="getImageUrl(item.imageUrlBefore)"
:username="item.username"
:date="item.createdAt"/> This is where I get the date
</template>
Is there anyway that I can retrieve the date only, rather than the date and time?
I am assuming you don't have access to the api and therefore have to process the date in your vue application.
Do you need the date as a date Object or is a string fine?
If a string representation is enough, you could use a library such as Date fns and use the format function:
...
:date="format(new Date(item.createdAt), 'yyyy-MM-dd')"
Another option might be to only use the first 10 characters of the string you received as the date: date="item.createdAt.substring(0,9)"
Did you try something like: :date="item.createdAt.substr(0, 10)"
In my experience the easiest way to format datetimes in JS it to use the Moment.js library (https://momentjs.com/). You could reformat the datetime string before passing it to your child component.
Or, if you don't want to rely on a third-party library and you know that the datetime string will always be passed in that format, I suppose you could do item.createdAt.slice(0, 10).
Moment.js can be very helpful with dates and times
Moment Docs
moment("String").format('L'); // 04/11/2020

need to convert MMDDYYYY string to date using momentjs

I am trying to convert string "12012019" to date format using momentjs instead of doing any string manipulation.
Does anyone have any idea or suggestion?
You can read the doc
moment('12012019', 'MMDDYYYY') // Moment instance, you can call .toDate() if you want to convert to native Date

Working with date formats in Java and MySQL

Ok this is really bugging me.
I am developing a web app and I need to work with dates. When a date is displayed in a view, or whenever a date is entered into a form I need the format to be dd/mm/yyyy.
What data type do I choose for my SQL database columns which contain dates. 'Date' doesn't seem to work, do I use varchar?
But If I use varchar how do I use java script to perform arithmetic with dates.
Do I do some conversions server-side?
Please advise the best practices.
Also Im using laravel if theres any useful stuff already built in.
Date is the correct type to use in SQL DB.
To access the value use ISO date format "YYYY-MM-DD HH:mm:ss" .
You can create it from Java Date Object by using toISOString() method.
For easier time format conversion I can also recommend to check out Moment.js.
Best practice to save the date in MySQL table as date field only. Which saves the date string in YYYY-MM-DD HH:mm:ss format.
You need to make sure the following things.
Before inserting date into MySQL change the format of date string to YYYY-MM-DD HH:mm:ss.
When you retrieve the date from database convert the date string from YYYY-MM-DD HH:mm:ss to your desired format.
You can use SimpleDateFormat class in Java to convert the dates
format. Use format() function to format the date in desired and
parse() function to get the Java date object from string.
Reference: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
You should use varchar2 in mysql.
You can retrieve that varchar type date in javascript and create date object.
var d=dbdate;
var date = new Date(d);
And you can perform all javascript functions on date.
Hope it will help.
Javascript Date() object supports separate entries of date by:
var date=new Date();
var y=date.getFullYear();//4 digits
var m=date.getMonth()+1;//0-11 digits, plus 1 for true state
var d=date.getDate();//1-31
var dateSQL=y+'-'+m+'-'+d;//i.e 2016-07-25
you can use MySQL filed as datetime and also insert datetime format but when you will show then process it as you want as like
when you insert value in table then you can also format it as like
<?php $mysqltime = date ("Y-m-d H:i:s", $phptime); ?>
where $phptime is your input variable
$str = suppose $row['date'] (mysql filed value)
date("d/m/Y", strtotime($str));
where $str your retrieve date filed

How to parse date time in JavaScript

I have Date in this format mm/dd/yy example: 04/11/13
and time in the format HH:MM:SS example: 17:02:30
I have to parse above two values and put in a variable dateTime with following format
YYYY-MM-DDTHH:MM:SSS
2013-04-11T17:02:30.000
What is best way to do it in AngularJS or in Javascript. I also need to verify the user input and make sure it is a valid mm/dd/yy date and a valid HH:MM:SS time
I know there are tons of duplicate/similar questions but I couldn't find one which answers above, please let me know if you found one.
You don't need an external library to do this. See this doc link for the forms of date that JavaScript can process normally.
For a specific solution to your question:
var date = new Date("04/11/13" + " " + "17:02:30");
date.toISOString();
>> "2013-04-11T21:02:30.000Z"
See this MDN page for more info on the Date object.
The best way to do this in AngularJS is with a filter. You bind to the dateTime, and then filter it with the date filter.
<span>{{myDate | date:yyyy-MM-ddThh:mm:sss}}</span>
Or in your controller you say:
$filter('date')(date[, format])
Here is more info: http://docs.angularjs.org/api/ng.filter:date

Categories